How to get php to read xml document? [duplicate] - php

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.

Related

How to get data from one tag of xml via php like script bellow? [duplicate]

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

How to read xml file as it is in browser using php? [duplicate]

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");

PHP SimpleXML raw content of node [duplicate]

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>

XML Data Fetch help need [duplicate]

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?

XML : PHP echo is empty [duplicate]

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']

Categories