Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8542581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:06:13+00:00 2026-06-11T12:06:13+00:00

I am attempting to display data an XML source, in a tree format, to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:06:15+00:00Added an answer on June 11, 2026 at 12:06 pm

    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:

    $xslt=new SimpleXMLElement('<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
       <xsl:template match="*">
           <ul>
                <li><xsl:value-of select="local-name()"/>
                 <xsl:apply-templates/>
                </li>
           </ul>
        </xsl:template>
    </xsl:stylesheet>');
    

    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):

    $content = file_get_contents('http://www.fluffyduck.com.au/sampleXML.txt'); 
    $xml = new SimpleXMLElement($content); 
    $xsl_processor = new XSLTProcessor(); 
    $xsl_processor->importStylesheet($xslt); 
    echo $xsl_processor->transformToXml($xml);
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem when binding the data in gridview. I am attempting to
I am currently attempting to implement a custom gridview interface to display data from
We're attempting to display two series of data on the same chart. The first
im attempting to display a value from a many to many relationship in my
I am attempting to display a very large graphical representation of some data. I
I am attempting to automate the entry of data into form fields. The problem
I'm currently attempting to use the Windows Powerpack DataRepeater control to display data from
attempting a first Blackberry App. It will display diary data (eventually). I'm just trying
I'm attempting to display my data, which is in a UITableView, in custom cells
I am using jsTree to display a tree structure of hierarchical data within my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.