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.
Related
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 5 years ago.
I have the following XML output from my website:
<?xml version="1.0" encoding="UTF-8"?>
<result>
<status>0</status>
<item>
<message>OK</message>
<id>123</id>
</item>
</result>
I'd like to grab the value inside <id> & store in a variable.
Is there a way to do this without using SimpleXML?
Thank you
There are a lot of ways to do this:
See the official documentation:
XML Manipulation : http://docs.php.net/manual/en/refs.xml.php
The problem is they are all much much more complicated than SimpleXML and nowhere as fast for random access
I would go with simplexml like this:
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<result>
<status>0</status>
<item>
<message>OK</message>
<id>123</id>
</item>
</result>';
$xmlcont = new SimpleXMLElement($xml);
echo $xmlcont->item->id;
Output : 123
Refer This
Using DOM
Using simplexml_load_file
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>
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 exists:
How do I enable error reporting in PHP? [duplicate]
Closed 8 years ago.
Hello everyone I am trying to create a XML file as a string in php so then I can curl it up to our system for processing.
How come I can't just do this?
$XML = ' <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
All of the php code below seems to not want to be picked up anymore after this.
Why does this happen since the entire string is surrounded by ' ' ?
If there is a way around this please let me know.
Use the heredoc syntax :
<?php
$str = <<<XML
<?xml version="1.0" encoding...
<partlist>
...
</babelpart>
XML;