How to get the names of all nodes and attributes of an xml in php? -
i'm using following code xml object of file:
$xml = simplexml_load_file($tmp_dir);
and after long research found how values of nodes using foreach loop, there way names? need can parse xml magicparse (http://www.magicparser.com/)
example input:
<?xml version="1.0"?> <root attribute="example_attribute"> <node 1> <nested node> <nested node> </node 1> <node 2> </node 2> </root>
desire output:
root -@attribute -node 1 -node 1/nested node -node 2
this node , attribute names
<?php $xf = file_get_contents($xmlfilename); $xml = simplexml_load_string($xf); displaynode($xml, 0); function displaynode($node, $offset) { if (is_object($node)) { $node = get_object_vars($node); foreach ($node $key => $value) { echo str_repeat(" ", $offset) . "-" . $key . "\n"; displaynode($value, $offset + 1); } } elseif (is_array($node)) { foreach ($node $key => $value) { if (is_object($value)) displaynode($value, $offset + 1); else echo str_repeat(" ", $offset) . "-" . $key . "\n"; } } }
Comments
Post a Comment