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?
Related
This question already has answers here:
How to parse XML with namespace?
(1 answer)
Parse XML namespaces with php SimpleXML
(2 answers)
Closed 2 years ago.
so I have a XML invoice and I want to access the data in it and I am absolutely clueless on how to do it. I tried following various guides online and nothing works, I get zero errors but zero results.
How can I get data from "inv:invoiceType" tag of this xml file? Thanks!
The XML is this:
<?xml version="1.0" encoding="UTF-8"?>
<dat:dataPack id="fa001" application="StwTest" version="2.0" note="Import" xmlns:dat="http://www.stormware.cz/schema/version_2/data.xsd" xmlns:inv="http://www.stormware.cz/schema/version_2/invoice.xsd" xmlns:typ="http://www.stormware.cz/schema/version_2/type.xsd">
<dat:dataPackItem id="20007" version="2.0">
<inv:invoice version="2.0">
<inv:invoiceHeader>
<inv:invoiceType>issuedInvoice</inv:invoiceType>
<inv:number>
<typ:numberRequested>20007</typ:numberRequested>
</inv:number>
<inv:paymentType>
<typ:paymentType>draft</typ:paymentType>
</inv:paymentType>
<inv:carrier>
<typ:ids>magic horse</typ:ids>
</inv:carrier>
<inv:numberOrder>20007</inv:numberOrder>
<inv:symVar>20007</inv:symVar>
<inv:date>2020-05-11</inv:date>
<inv:dateTax>2020-05-13</inv:dateTax>
<inv:dateDue>2020-05-27</inv:dateDue>
</inv:invoiceHeader>
</inv:invoice>
</dat:dataPackItem>
</dat:dataPack>
try using SimpleXML if you are not using it.
you can follow this document : Basic SimpleXML usage
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:
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 reading question (PHP)
(3 answers)
Closed 9 years ago.
I need some php code to read this XML, and take the content of the <title>
tags and declare as a $title variable. And the same for the <tags> and <content> as $tags and $content variables respectively.
The XML is listed below:
<?xml version="1.0" encoding="ISO-8859-1"?>
<page>
<title>Example title</title>
<tags>example,title,page</tags>
<content>This page has some lovely example content</content>
</page>
this has been talked many times on stackoverflow, try to learn about simplexml. you need to load the file and then loop inside
$data = simplexml_load_string($xml);
foreach($data as $key => $val)
echo "$key=>$val" . "<br>";
live working sample -> http://codepad.viper-7.com/6r1Tnd
You need to read loadXML. The link describes this function and gives some example code on how to use it.