0

I want to read nested elements and each element's attribute values from an XML file. I am using simpleXML and here is my code so far:

$path = Mage::getBaseUrl('media').'Banners/data.xml' ;
$doc = new DOMDocument();
$doc->load( $path );

$items = $doc->getElementsByTagName( "imgitem" );
$xml=simplexml_load_file($path);

foreach($xml->children() as $child)
{
  foreach( $child as $item )
    {
        $attr = $xml->imgitem->attributes();
        echo $attr['image'].'<br>';
        echo $attr['link'].'<br>';
        echo $attr['target'].'<br>';            

    }
}

Can anyone point me in the right direction in order to get all the child nodes and their attributes recursively?

Here is the source XML file structure

<Banner> 
<imgitem image="img/source/path" link="" target="_blank" delay="" textBlend="yes"> <![CDATA[Autumn Leaves]]> </imgitem> 
<imgitem image="img/source/path" link="" target="_blank" delay="" textBlend="yes"> <![CDATA[Creek]]> </imgitem> 
<imgitem image="img/source/path" link="" target="_blank" delay="" textBlend="yes"> <![CDATA[Dock]]> </imgitem> 
</Banner>

Now what I want is the values of all the attributes of each of the three items of the parent tag. So need to loop through the main tag to get all three (or any number of) items and then take another loop and fetch all the attribute values of each item.

1
  • You appear to have some DOM code in there which isn't doing anything - perhaps left over from a previous attempt? Also, it's not entirely clear where the recursion would fit in given the code you show; can we see a sample of the XML you are trying to process and the desired output? Commented Jul 3, 2013 at 13:16

3 Answers 3

2

Was looking for similar solution for finding all child node recursively up to infinite in a large xml document. This worked for me:

 $xmlIterator = new RecursiveIteratorIterator(
    new SimpleXMLIterator($xml_string), 
    RecursiveIteratorIterator::SELF_FIRST
 );
 foreach ($xmlIterator as $nodeName => $node) {
   echo "<br>" . $nodeName, $node['attribute'], PHP_EOL;
 }
Sign up to request clarification or add additional context in comments.

2 Comments

there is one ) missing.
@Agguro Well spotted; it looks like a previous formatting improvement accidentally moved the bracket onto the wrong line. For future reference, you can edit any question or answer to fix problems like this.
1

Thanks for clarifying the question. I think you're over-complicating things here - there is no recursion needed here, and you may not even need more than one loop.

You already have foreach($xml->children() as $child) - this will give you each <imgitem> in turn as $child, since they are the children of the top-level item. You could equivalently say foreach($xml->imgitem as $child), which would skip any children which are not <imgitem> tags.

To echo attributes of those elements, you just need to say $child['someAttributeName'] - so your code as you have in the example would just look like this:

$path = Mage::getBaseUrl('media').'Banners/data.xml' ;
$xml = simplexml_load_file($path);

foreach($xml->children() as $child)
{
    echo $child['image'].'<br>';
    echo $child['link'].'<br>';
    echo $child['target'].'<br>';           
}

If instead of referencing particular attributes, you for some reason want to loop over every attribute in turn, you just need one more loop inside:

foreach($xml->children() as $child)
{
    foreach($child->attributes() as $attr_name => $attr_value)
    {
        echo "Attribute '$attr_name' has value '$attr_value'";
    }
}

Comments

0

This example works if you add a second ) before ; in the first line of code. It lists all nodes from the root of the XML file and all it's childs.

$xmlIterator = new RecursiveIteratorIterator(new SimpleXMLIterator($xmlstring,RecursiveIteratorIterator::SELF_FIRST));
foreach ($xmlIterator as $nodeName => $node)
{
    echo "<br>" . $nodeName, $node['value'], PHP_EOL;
}

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.