Iterating over an unknown XML structure using PHP (DOM)

I want to write a function that parses (theoretically) an unknown XML data structure in an equivalent PHP array.

Here is my XML example:

<?xml version="1.0" encoding="UTF-8"?> <content> <title>Sample Text</title> <introduction> <paragraph>This is some rudimentary text</paragraph> </introduction> <description> <paragraph>Here is some more text</paragraph> <paragraph>Even MORE text</paragraph> <sub_section> <sub_para>This is a smaller, sub paragraph</sub_para> <sub_para>This is another smaller, sub paragraph</sub_para> </sub_section> </description> </content> 

I changed this DOM repeat function from devarticals:

 $data = 'path/to/xmldoc.xml'; $xmlDoc = new DOMDocument(); #create a DOM element $xmlDoc->load( $data ); #load data into the element $xmlRoot = $xmlDoc->firstChild; #establish root function xml2array($node) { if ($node->hasChildNodes()) { $subNodes = $node->childNodes; foreach ($subNodes as $subNode) { #filter node types if (($subNode->nodeType != 3) || (($subNode->nodeType == 3))) { $arraydata[$subNode->nodeName]=$subNode->nodeValue; } xml2array($subNode); } } return $arraydata; } //The getNodesInfo function call $xmlarray = xml2array($xmlRoot); // print the output - with a little bit of formatting for ease of use... foreach($xmlarray as $xkey) { echo"$xkey<br/><br/>"; } 

Now, due to the way I pass elements to an array, I rewrite any elements that share the node name (since I ideally want to give the keys the same names as their original nodes). My recursion is small ... However, even if I release the brackets, the second level of nodes is still included as values ​​in the first level (see text for the description of node).

Anyone have any ideas how I can better build this?

+6
arrays xml loops php parsing
source share
3 answers
0
source share

You might be better off building some kind of code from the network.

http://www.bin-co.com/php/scripts/xml2array/

  /** * xml2array() will convert the given XML text to an array in the XML structure. * Link: http://www.bin-co.com/php/scripts/xml2array/ * Arguments : $contents - The XML text * $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value. * $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance. * Return: The parsed XML in an array form. Use print_r() to see the resulting array structure. * Examples: $array = xml2array(file_get_contents('feed.xml')); * $array = xml2array(file_get_contents('feed.xml', 1, 'attribute')); */ function xml2array($contents, $get_attributes=1, $priority = 'tag') { 
+2
source share

You may be interested in SimpleXML or xml_parse_into_struct .

$ arraydata is not passed to subsequent xml2array () calls and the return value is not used, so yes "My recursion is not great ..." is true ;-)
To add a new element to an existing array, you can use empty square brackets, $ arr [] = 123; $ arr [$ x] [] = 123;

+1
source share

All Articles