When given multiple nodes, hint how to access each as [0], [1], etc

This commit is contained in:
Rowan Collins 2012-10-14 01:21:22 +01:00
parent 5231f710de
commit d05192edb6

View file

@ -35,7 +35,6 @@ function simplexml_tree(SimpleXMLElement $sxml, $include_string_content=false, $
while ( isset($sxml[$root_item_index]) )
{
$root_item = $sxml[$root_item_index];
$root_item_index++;
// Special case if the root is actually an attribute
// It's surprisingly hard to find something which behaves consistently differently for an attribute and an element within SimpleXML
@ -50,12 +49,12 @@ function simplexml_tree(SimpleXMLElement $sxml, $include_string_content=false, $
$dump .= key($ns) . ':';
}
$dump .= $root_item->getName() . '="' . (string)$root_item . '"' . PHP_EOL;
// Jump to begining of outer loop; avoids indenting the rest of the code as an else block
continue;
}
else
{
// Display the root node as a numeric key reference, plus a hint as to its tag name
// e.g. '[42] // <Answer>'
// Display the root node as an XML tag
// To what namespace does this attribute belong? Returns array( alias => URI )
$ns = $root_item->getNamespaces(false);
if ( key($ns) )
@ -66,7 +65,7 @@ function simplexml_tree(SimpleXMLElement $sxml, $include_string_content=false, $
{
$root_node_name = $root_item->getName();
}
$dump .= "<$root_node_name>" . PHP_EOL;
$dump .= "[$root_item_index] // <$root_node_name>" . PHP_EOL;
// This function is effectively recursing depth-first through the tree,
// but this is managed manually using a stack rather than actual recursion
@ -77,6 +76,9 @@ function simplexml_tree(SimpleXMLElement $sxml, $include_string_content=false, $
);
}
$root_item_index++;
}
// Add on the header line, with the total number of items output
$dump = 'SimpleXML object (' . $root_item_index . ' item' . ($root_item_index > 1 ? 's' : '') . ')' . PHP_EOL . $dump;