0

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');
}
?>

1 Answer 1

1

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:

$php_object = SimpleXMLElement($xml_string);
var_dump($php_object);
exit;

And this will allow you to look at the structure of your object and decide how best to access it. Good luck.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks :-) But still i'm not sure how to read out the "Name" value (I'm not very expereinced in XML). Something like $strValue = $php_object->NAME
Got it $node->PROP[0]->attributes()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.