I am not very sure why my inner loop data is added to the external loop data-
XML I am parsing - http://pastebin.com/vGc5NhXr
Code I am using -
<?php
$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->load('course/Golf/imsmanifest.xml');
// get the resources element
$organization = $dom->getElementsByTagName( "item" );
echo '<ul>';
foreach( $organization as $organizationItem )
{
$unitTitle = $organizationItem->getElementsByTagName("title");
$unitName = $unitTitle->item(0)->nodeValue;
echo '<li>',$unitName,'</li>';
echo '<ul>';
$item1 = $organizationItem->getElementsByTagName( "item" );
foreach( $item1 as $myitem ) {
$title = $myitem->getElementsByTagName("title");
$author = $title->item(0)->nodeValue;
echo '<li>',$author,'</li>';
}
echo '</ul>';
}
echo '</ul>';
Generated output - http://codepad.org/J2vP71rd
Expected Output - http://codepad.org/uzUtehgT
Let me know what I am doing wrong with the for each loop.
Because the item
elements are nested. $dom->getElementsByTagName( "item" )
gets all the item
elements, including those lie within another item
. That's not what you want.
I'd suggest using XPath for this kind of job.