How do i read out the “Name” value for the PROP’s
I can read the PVAL’s using the script below..
<?xml version='1.0'?>
<RECORDS>
<RECORD>
<PROP NAME="Product">
<PVAL><![CDATA[Produkt1]]></PVAL>
</PROP>
<PROP NAME="Value">
<PVAL><![CDATA[10]]></PVAL>
</PROP>
<PROP NAME="Status">
<PVAL><![CDATA[Active]]></PVAL>
</PROP>
</RECORD>
<RECORD>
<PROP NAME="Product">
<PVAL><![CDATA[Produkt2]]></PVAL>
</PROP>
<PROP NAME="Value">
<PVAL><![CDATA[20]]></PVAL>
</PROP>
<PROP NAME="Status">
<PVAL><![CDATA[Active]]></PVAL>
</PROP>
</RECORD>
<RECORD>
<PROP NAME="Product">
<PVAL><![CDATA[Produkt3]]></PVAL>
</PROP>
<PROP NAME="Value">
<PVAL><![CDATA[30]]></PVAL>
</PROP>
<PROP NAME="Status">
<PVAL><![CDATA[Active]]></PVAL>
</PROP>
</RECORD>
Using the script below i can get the PVAL’s.
But i would like to get the name values as well, i have tried
$strvalue = $node->PROP[6]->NAME; without luck…
<?php
$z = new XMLReader;
$z->open('products.xml');
$doc = new DOMDocument;
// move to the first <product /> node
while ($z->read() && $z->name !== 'RECORD');
// now that we're at the right depth, hop to the next <product/> until the end of the tree
while ($z->name === 'RECORD')
{
$node = simplexml_import_dom($doc->importNode($z->expand(), true));
$strvalue = $node->PROP[6]->PVAL;
echo $strvalue."<p>" ;
// go to next <product />
$z->next('RECORD');
}
?>
Here is a suggestion: use
SimpleXMLElement($xml_string)this takes an xml string as input and returns a PHP object that you can work with more easily. Then you can do handy stuff like:And this will allow you to look at the structure of your object and decide how best to access it. Good luck.