Question:
how to read this xml file with php ?
another_man
2009-06-02 07:44:29 UTC
Using any method... DOMDocument with foreach if possible, but any other way is good!



Node Value
Node Value
...


Node Value
Node Value
...



Explanation : I have a xml file with the main node (xml) in which I have two child nodes (node1 and node2) in which I have lots of child nodes (node) with their values

Thanks a lot
Four answers:
topherG
2009-06-02 08:33:26 UTC
We'll say that your XML file name is doc.xml and the XSL file is doc.xsl...




$xml_file = 'doc.xml';

$xsl_file = 'doc.xsl';



$doc = new DOMDocument();

$xsl = new XSLTProcessor();



$doc->load($xsl_file);

$xsl->importStyleSheet($doc);



$doc->load($xml_file);

echo $xsl->transformToXML($doc);

?>



Note that the XSL file contains the style information for your XML file. This file tells the XML file how to render on the screen. If you do not have an XSL file, then you can either create one or remove the XSL pieces of the above PHP.
Kris
2009-06-02 08:05:51 UTC
Try this, it will generate an array from the XML file. (From the php.net website)



Usage:

$file = 'file.xml';

$xmlArray = genArrayFromXML($file);



Then use foreach to get the data back out, you will have to use more then one foreach to reach each node.



Example:

foreach($xmlArray as $a)

{

foreach($a as $b)

{

foreach($b as $c)

{



// Node data for value of "node"

}

}

}

?>



Sorry it got cut off take a look here: http://www.w3yxe.org/xml.txt
anonymous
2009-06-02 07:53:07 UTC
Don't re invent the wheel.



Try XML Parser.



http://us2.php.net/manual/en/book.xml.php
anonymous
2016-10-06 09:54:48 UTC
$xmlDoc = new DOMDocument(); $xmlDoc->load("xml.xml"); $x = $xmlDoc->documentElement; foreach ($x->childNodes AS $merchandise) { foreach ($merchandise->childNodes AS $y) { echo $y->nodeName . " = " . $y-nodeValue; } } i think of that something like which will remedy it. pass to the region and study some extra for added info on what you attempt to end.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...