I am attempting to display data an XML source, in a tree format, to display correct Parent -> Child relationship.
The problem is the child nodes can have multiple child’s themselves (there is no limit)
An example of the output that works in flash = http://fluffyduck.com.au/example-layout.jpg
I cannot replicate this in PHP / html however.
I have provided an example XML source here : http://www.fluffyduck.com.au/sampleXML.txt
With the help of some other people, a list of parent ID’s and their children, as follows :
<?php
$content = file_get_contents('http://www.fluffyduck.com.au/sampleXML.txt');
$xml = new SimpleXMLElement($content);
$users = array ();
myUserReader($xml->user, $users);
function myUserReader($node, &$users)
{
if (array_key_exists("{$node['id']}", $users) === false)
{
$users["{$node['id']}"] = array ();
}
if (isset($node->user))
{
for ($key = 0; ($key < count($node->user)); $key++)
{
$user = $node->user[$key];
if (!in_array("{$node['id']}", $users["{$node['id']}"]))
{
$users["{$node['id']}"][] = "{$user['id']}";
# $users["{$node['id']}"][] = $user;
}
myUserReader($user, $users);
}
}
}
foreach ($users as $parent => $children)
{
echo "{$parent}: " . implode(", ", $children) . "\n";
}
?>
I am unable to work out what code I need to parse the above results and display the information as per the attached image above.
I have contemplated converting the XML to an array, but still have the same trouble, the code I have tried for that is :
function xmlstr_to_array($xmlstr) {
$doc = new DOMDocument();
$doc->loadXML($xmlstr);
return domnode_to_array($doc->documentElement);
}
function domnode_to_array($node) {
$output = array();
switch ($node->nodeType) {
case XML_CDATA_SECTION_NODE:
case XML_TEXT_NODE:
$output = trim($node->textContent);
break;
case XML_ELEMENT_NODE:
for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
$child = $node->childNodes->item($i);
$v = domnode_to_array($child);
if(isset($child->tagName)) {
$t = $child->tagName;
if(!isset($output[$t])) {
$output[$t] = array();
}
$output[$t][] = $v;
}
elseif($v) {
$output = (string) $v;
}
}
if(is_array($output)) {
if($node->attributes->length) {
$a = array();
foreach($node->attributes as $attrName => $attrNode) {
$a[$attrName] = (string) $attrNode->value;
}
$output['@attributes'] = $a;
}
foreach ($output as $t => $v) {
if(is_array($v) && count($v)==1 && $t!='@attributes') {
$output[$t] = $v[0];
}
}
}
break;
}
return $output;
}
$moo = xmlstr_to_array($content);
print_r($moo);
However the same problem exists, I do not know how to cycle through and display the information in the correct Parent -> Child manner recursively.
Due to the way the data is constructed I also cannot work out how to retrieve a “single” entry out, if i could do that perhaps I could manually rebuild the tree, I’m not sure how as each record does not have a unique identifer.
Any help greatly appreciated.
Might be much better in this case to avoid DOM or SimpleXML, and instead use XSLT, as it was designed for handling recursion. Create a stylesheet, something like this:
Then transform it using the built in XSL libraries for PHP (note that some machines may have compiled PHP to not have this built in, but it’s likely it is):
That’s just a simple case, but it should generate a long, unordered list of all the parents and their children, properly nested. Modify the “value-of” directive to pull out attributes, etc. that you may rather display instead.