Skip to content

Commit d05192e

Browse files
committed
When given multiple nodes, hint how to access each as [0], [1], etc
1 parent 5231f71 commit d05192e

1 file changed

Lines changed: 23 additions & 21 deletions

File tree

simplexml_tree.php

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ function simplexml_tree(SimpleXMLElement $sxml, $include_string_content=false, $
3535
while ( isset($sxml[$root_item_index]) )
3636
{
3737
$root_item = $sxml[$root_item_index];
38-
$root_item_index++;
3938

4039
// Special case if the root is actually an attribute
4140
// It's surprisingly hard to find something which behaves consistently differently for an attribute and an element within SimpleXML
@@ -50,31 +49,34 @@ function simplexml_tree(SimpleXMLElement $sxml, $include_string_content=false, $
5049
$dump .= key($ns) . ':';
5150
}
5251
$dump .= $root_item->getName() . '="' . (string)$root_item . '"' . PHP_EOL;
53-
54-
// Jump to begining of outer loop; avoids indenting the rest of the code as an else block
55-
continue;
56-
}
57-
58-
// Display the root node as an XML tag
59-
// To what namespace does this attribute belong? Returns array( alias => URI )
60-
$ns = $root_item->getNamespaces(false);
61-
if ( key($ns) )
62-
{
63-
$root_node_name = key($ns) . ':' . $root_item->getName();
6452
}
6553
else
6654
{
67-
$root_node_name = $root_item->getName();
55+
// Display the root node as a numeric key reference, plus a hint as to its tag name
56+
// e.g. '[42] // <Answer>'
57+
58+
// To what namespace does this attribute belong? Returns array( alias => URI )
59+
$ns = $root_item->getNamespaces(false);
60+
if ( key($ns) )
61+
{
62+
$root_node_name = key($ns) . ':' . $root_item->getName();
63+
}
64+
else
65+
{
66+
$root_node_name = $root_item->getName();
67+
}
68+
$dump .= "[$root_item_index] // <$root_node_name>" . PHP_EOL;
69+
70+
// This function is effectively recursing depth-first through the tree,
71+
// but this is managed manually using a stack rather than actual recursion
72+
// Each item on the stack is of the form array(int $depth, SimpleXMLElement $element, string $header_row)
73+
$dump .= _simplexml_tree_recursively_process_node(
74+
$root_item, 1,
75+
$include_string_content, $indent, $content_extract_size
76+
);
6877
}
69-
$dump .= "<$root_node_name>" . PHP_EOL;
7078

71-
// This function is effectively recursing depth-first through the tree,
72-
// but this is managed manually using a stack rather than actual recursion
73-
// Each item on the stack is of the form array(int $depth, SimpleXMLElement $element, string $header_row)
74-
$dump .= _simplexml_tree_recursively_process_node(
75-
$root_item, 1,
76-
$include_string_content, $indent, $content_extract_size
77-
);
79+
$root_item_index++;
7880
}
7981

8082
// Add on the header line, with the total number of items output

0 commit comments

Comments
 (0)