0

I have a XML with the following structure:

<item>
                      <attributes>
                <attribute datatype="text">
                    <name><![CDATA[name]]></name>
                    <value><![CDATA[Product Name]]></value>
                </attribute>
                <attribute datatype="text">
                    <name><![CDATA[sku]]></name>
                    <value><![CDATA[PRODUCTSKU]]></value>
                </attribute>
                <attribute datatype="numeric">
                    <name><![CDATA[price]]></name>
                    <value>10000</value>
                </attribute>
        <attribute datatype="numeric">
                    <name><![CDATA[eancode]]></name>
                    <value>123456789</value>
                </attribute>
            </attributes>
        </item>

Now, here I am looking for the following output:

Product Name : PRODUCTSKU : 10000 : 123456789

But with the following code am fetching the whole details under attribute node but I can't segregate the SUB NODES values...

$reader = new XMLReader();
$reader->open( 'test.xml' );
$id = 'attributes';
while ($reader->read()) {
    if ($reader->nodeType == XMLReader::ELEMENT) {
        $exp = $reader->expand();
        if ($exp->nodeName == $id)
            echo "<b>" . $exp->nodeName . " : </b>" . $exp->nodeValue . "<br />";

    }
}

I have also tried this with DOMDOcument also:

$xml = new DOMDocument(); 
$xml->load('test.xml');
$root = $xml->documentElement;
foreach($root->childNodes as $node){

    //print $node->nodeName.' - '.$node->nodeValue;
    print $node->nodeValue;
}

But this also displays all the sub nodes together...I want to segregate them and store into DB...please help me with this...am struggling with this quiet a long

5
  • I'm not familiar with the XMLReader class but can you call the expand function again to go a level deeper? Commented Apr 23, 2015 at 8:55
  • @scrowler...am also not very familiar with this...am using XMLReader since the file is too large and XMLReader is the best option to read such file Commented Apr 23, 2015 at 8:58
  • Can you call the expand function within your if statement again to go a level deeper? You might consider changing to use SimpleXML or DOMDocument which are also both native to PHP Commented Apr 23, 2015 at 9:01
  • @scrowler...any idea how to use DOMDocument here? Commented Apr 23, 2015 at 9:05
  • @scrowler...please see my edit in the post... Commented Apr 23, 2015 at 9:15

1 Answer 1

2

It's easier to use SimpleXML but if you want to use XMLReader try this:

$values = array();
$reader = new XMLReader();
$reader->open( 'test.xml' );
$id = 'attribute';
while ($reader->read()) {
    if ($reader->nodeType == XMLReader::ELEMENT) {
        $exp = $reader->expand();
        if ($exp->nodeName == "item") {
            if (count($values)) print implode(" : ", $values);
            $values = array();
        }
        if ($exp->nodeName == $id) {
            foreach ($exp->childNodes as $node) {
                if ($node->nodeName == "value") {
                    $values[] = $node->textContent;
                }
            }
        }
    }
}
if (count($values)) print implode(" : ", $values);
Sign up to request clarification or add additional context in comments.

Comments

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.