From ee77e63662c455494c501df760162575f15acdf8 Mon Sep 17 00:00:00 2001 From: Rowan Collins Date: Sun, 2 Sep 2012 21:21:55 +0100 Subject: [PATCH] Initial prototype 20/08/2012 23:25 --- simplexml_dump.php | 113 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 simplexml_dump.php diff --git a/simplexml_dump.php b/simplexml_dump.php new file mode 100644 index 0000000..e5e1419 --- /dev/null +++ b/simplexml_dump.php @@ -0,0 +1,113 @@ +getNamespaces(false); + if ( $ns ) + { + $dump .= $indent . $indent . 'Namespace: \'' . reset($ns) . '\'' . PHP_EOL; + $dump .= $indent . $indent . 'Namespace Alias: \'' . key($ns) . '\'' . PHP_EOL; + } + + $dump .= $indent . $indent . 'Name: \'' . $item->getName() . '\'' . PHP_EOL; + $dump .= $indent . $indent . 'Value: \'' . (string)$item . '\'' . PHP_EOL; + + $dump .= $indent . '}' . PHP_EOL; + } + else + { + $dump .= $indent . 'Element {' . PHP_EOL; + + $ns = $item->getNamespaces(false); + if ( $ns ) + { + $dump .= $indent . $indent . 'Namespace: \'' . reset($ns) . '\'' . PHP_EOL; + if ( key($ns) == '' ) + { + $dump .= $indent . $indent . '(Default Namespace)' . PHP_EOL; + } + else + { + $dump .= $indent . $indent . 'Namespace Alias: \'' . key($ns) . '\'' . PHP_EOL; + } + } + + $dump .= $indent . $indent . 'Name: \'' . $item->getName() . '\'' . PHP_EOL; + $dump .= $indent . $indent . 'String Content: \'' . (string)$item . '\'' . PHP_EOL; + + // Now some statistics about attributes and children, by namespace + $dump .= $indent . $indent . 'Structural Content:' . PHP_EOL; + + // If the default namespace is not declared, it will never show up using the below code + // (This is probably bad XML, or at least bad practice, but the aim here is to leave nothing invisible) + if ( ! array_key_exists('', $item->getDocNamespaces()) ) + { + $dump .= $indent . $indent . $indent . '[Default, Undeclared Namespace]' . PHP_EOL; + + $dump .= $indent . $indent . $indent . $indent . 'Children: ' . count($item->children(NULL)) . PHP_EOL; + $dump .= $indent . $indent . $indent . $indent . 'Attributes: ' . count($item->attributes(NULL)) . PHP_EOL; + } + + $all_ns = $item->getNamespaces(true); + foreach ( $all_ns as $ns_alias => $ns_uri ) + { + $ns_label = (($ns_alias == '') ? '[Default Namespace]' : "Namespace $ns_alias"); + $dump .= $indent . $indent . $indent . $ns_label . PHP_EOL; + + $dump .= $indent . $indent . $indent . $indent . 'Namespace URI: \'' . $ns_uri . '\'' . PHP_EOL; + $dump .= $indent . $indent . $indent . $indent . 'Children: ' . count($item->children($ns_uri)) . PHP_EOL; + $dump .= $indent . $indent . $indent . $indent . 'Attributes: ' . count($item->attributes($ns_uri)) . PHP_EOL; + } + + $dump .= $indent . '}' . PHP_EOL; + } + } + $dump .= ']' . PHP_EOL; + + // Add on the header line, with the total number of items output + $dump = 'SimpleXML object (' . $item_index . ' item' . ($item_index > 1 ? 's' : '') . ')' . PHP_EOL . $dump; + + echo $dump; +} + +$x = << + + some text + ooo + + + + + + +XML; + +$sx = simplexml_load_string($x); + +simplexml_dump($sx); +simplexml_dump($sx->children()); +simplexml_dump($sx->deeper); +simplexml_dump($sx->deeper->children('a', true)); +simplexml_dump($sx->bar[2]->attributes('b', true)); +simplexml_dump($sx->deeper['bob']); + +