This question already has answers here:
PHP SimpleXML get innerXML
(11 answers)
Closed 6 years ago.
I am using PHP SimpleXML to parse data in a file. Say for example I have the following XML content:
<?xml version="1.0" ?>
<root>
<parent1>
<child1>blah</child1>
<child2>blah blah</child2>
</parent1>
<parent2>blah</parent2>
</root>
I basically want to get the actual raw content of the inside of a node. I.e. I need it to return the "innerXML" of parent1 just as text, tags and all. Having trouble getting PHP to do this. Help?
For getting the exact content '<child1>blah</child1><child2>blah blah</child2>' as itself you better put them under <![CDATA[]]> Then you can get any thing inside that as iself.
xml should be like this::
<?xml version="1.0" ?>
<root>
<parent1>
<![CDATA[<child1>blah</child1>
<child2>blah blah</child2>]]>
</parent1>
<parent2>blah</parent2>
</root>
Then
$xml = simplexml_load_string($xmlstring, 'SimpleXMLElement', LIBXML_NOCDATA);
$parent1 = $xml->parent1->asXml();
echo "<pre>";echo ($parent1);echo "<pre>";
This will output :
<child1>blah</child1>
<child2>blah blah</child2>
Related
This question already has answers here:
How to echo XML file in PHP
(10 answers)
Closed 3 years ago.
I have created an xml file using DOMDocument. I want to read the xml file as it is in browser. If I do
$xml = simplexml_load_file('/var/lol/test.xml');
print_r($xml);
It displays the parsed object not the xml itself. I want the xml displayed as it is. How do I do this. I have tried file_get_contents() too. Doesn't work. Please help.
Use like below to show xml content as like it is on the web.
echo '<p>This is XML string content:</p>';
echo '<pre>';
echo htmlspecialchars(file_get_contents("test.xml"));
echo '</pre>';
test.xml content is like below
<?xml version="1.0"?>
<book>
<title>This is the title</title>
</book>
Use following header to display the XML:
header("Content-type: text/xml");
echo file_get_contents("/path/to/xml/file.xml");
This question already has answers here:
php parse xml string [duplicate]
(3 answers)
Closed 7 years ago.
Looking to parse some XML data that is saved in a DB. In the DB its saved in raw XML:
<?xml version="1.0" encoding="utf-8"?>
<RESPONSE>
<SUCCESS>true</SUCCESS>
<ERRORMESSAGE>
</ERRORMESSAGE>
<DATA>
</DATA>
</RESPONSE>
I want to be able to parse this data, and have found people normally using:
$xmlstr = <<<XML RAW XML XML;
But how can I use a php string in there?
So e.g.
$xmlstr = <<<XML $stringfromDB XML;
Of course that does not work, but how can it be done? Thanks.
SimpleXML should do the trick for you. Check out this link, along with php manual basics.
$xmlData = /* query xml-data from DB as string */;
$xmlObj = simplexml_load_string($xmlData);
var_dump($xmlObj);
the <<<XML ...some code... XML; is just an alternative notation for declaring (multiline) strings, you don't have to do that, as long as you can obtain full XML document from DB as in your first snippet.
This question already has answers here:
Simple XML - Dealing With Colons In Nodes
(4 answers)
Closed 8 years ago.
I have a XML file for my project work.I need to fetch the data from the XML file.The file contains something like the following:
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:szgd="http://www.spotzot.com/gdfeed" xmlns:szbb="http://www.spotzot.com/bbfeed">
<channel>
<item>
<title>Service & Maintenance under $50</title>
<description>7 Items | Prestone, Campbell Hausfeld, Custom Accessories, Custom, KD Tools</description>
<szgd:instore>Y</szgd:instore>
</item>
</channel>
</rss>
I am using $newtitle=$newxml->channel->item->title; to fetch data from title.It successfully returned the title.
How can I fetch the data from tag in my page using php?
Use php's simplexml functions.
Load it with:
$xml = simplexml_load_string($xml); if you are having a string or
$xml = simplexml_load_file($xmlFile); if you are having a file.
if the xml was valid you will get a SimpleXmlElement. Check the following link: http://php.net/manual/en/class.simplexmlelement.php. There you will find several functions on how to get data out of it.
Btw. which data you really want to get?
This question already has answers here:
How to get the value of an attribute from XML file in PHP?
(4 answers)
Closed 8 years ago.
I have an xml file that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
<response>
<result code="1000">
<msg lang="nl">De transactie is succesvol afgerond.</msg>
</result>
<trID>
<clTRID>300100</clTRID>
<svTRID>602C9E44-3F79-564D-5A53-C9689F088A1C</svTRID>
</trID>
</response>
</epp>
I need the result code from the xml in a string.
I already tried it with simpleXml :
$resCode = new \SimpleXMLElement($info);
$text = (string)$resCode->result;`
my xml is stored in $info
But this is not working. What am I doing wrong?
Your XML path is completely off. Try this according to the xml you posted:
$text = (string)$resCode->response->result["code"];
(Hint: the first node "epp" is not needed as it is the root node. So your path starts from inside this root node.)
Some samples:
http://www.php.net/manual/en/simplexml.examples-basic.php
This question already has an answer here:
Get root node of XML doc using simplexml
(1 answer)
Closed 8 years ago.
Having an issue echo'ing xml tags.
PHP:
$xml = $insureFormResult->returned;
$xml1 = new SimpleXMLElement($xml);
$result = $xml1->xpath("response")[0];
echo $result;
If I echo $xml it gives me:
<?xml version="1.0" encoding="utf-8"?>
<response>
<errors>
<error code="7">Your details are already in our system and have been forwarded to our insurance partners who will contact you shortly</error>
</errors>
</response>
The xml will always have one response tag. I also want to know how to echo the tag with id 'code'. I tried the php above but there's no result echoed.
Any kind of help will be appreciated!
EDIT
It wasn't working because of the version of PHP on my server.
I'm not really familiar with PHP, but the xpath to access the error with attribute code with value 7 is:
/response/errors/error[#code='7']