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

  • SEARCH
  • Home
  • 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 7646857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:19:01+00:00 2026-05-31T10:19:01+00:00

I am trying to read the xml from a file and display them just

  • 0

I am trying to read the xml from a file and display them just like a tree. I am using PHP and I want to get the output as shown below. The content of xml file is as follows…

<Categories>

<Category>
  <Id>1</Id>
  <Name>Parent 1</Name>
  <ParentId>1</ParentId>
  <ParentName>Parent 1</ParentName>
</Category>
<Category>
  <Id>2</Id>
  <Name>Child 1</Name>
  <ParentId>1</ParentId>
  <ParentName>Parent 1</ParentName>
</Category>
<Category>
  <Id>3</Id>
  <Name>Child 2</Name>
  <ParentId>1</ParentId>
  <ParentName>Parent 1</ParentName>
</Category>
<Category>
  <Id>8</Id>
  <Name>Grand Child 1 -1</Name>
  <ParentId>2</ParentId>
  <ParentName>Child 1</ParentName>
</Category>

<Category>
  <Id>12</Id>
  <Name>Parent 2</Name>
  <ParentId>12</ParentId>
  <ParentName>Parent 2</ParentName>
</Category>

<Category>
  <Id>15</Id>
  <Name>Child 2-1</Name>
  <ParentId>12</ParentId>
  <ParentName>Parent 2</ParentName>
</Category>

  </Categories>
</CategoryList>

I want to read this xml file (I know how to read it) But I can not format it like follows… How would I get all the nodes that are the top most parents and get child of those parent nodes (using recursion or what so ever)

<ul>
<li>Parent 1
    <ul>
        <li> Child 1
            <ul>
                <li>Grand Child 1 -1</li>
            </ul>

        </li>
        <li> Child 2</li>
    </ul>

</li>
<li>Parent 2
    <ul>
        <li>Child 2-1 </li>
    </ul>

</li>

</ul>

Please any help will be greatly appreciated….

Edit* What I have done so far…

$xml= simplexml_load_string('myxmlstring');

get_categories($xml, 0);

function get_categories($xml, $id) {

    if ($id==0) 
        $Categories = $xml->xpath('Categories/Category[ParentId=Id]');
    else
        $Categories = $xml->xpath('Categories/Category[ParentId='.$id.' and Id!='.$id.']');

    echo '<ul id="catlist'.$id.'">';
    foreach($Categories as $Category) {
        echo "<li>ID: " . $Category->Id . "--Name: " . $Category->Name;
        get_categories($xml, $Category->Id);
        echo "</li>";

    }
    echo "</ul>";
}

Now I just want to confirm that this is the optimal solution. or someone can come with better idea…

  • 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-05-31T10:19:03+00:00Added an answer on May 31, 2026 at 10:19 am

    ParentName is excessive, it’s enough to put just parent id.

    Since you do a search for each node, execution time will be O(N2), where N is amout of nodes.

    There’s an option to do this in linear time, though: Firstly traverse data and build tree structure (or somewhat) and then traverse that structure and output nodes according to it.

    Output buffering is also a good option here.

    // init
    $childrenReferences = array();
    $rootNodes = array();
    $xmlNodes = array();
    
    // gathering structure
    $cats = $xml->getElementsByTagName('Category');
    for ($i = 0; $i < $cats->length; $i++) {
        $cat = $cats[$i];
        $id = $children->Id;
        $parentId = $cat->ParentId;
        $xmlNodes[$id] = $cat;
        if ($parentId == $id) {
            $rootNodes []= $id;
            continue;
        }
        if (array_key_exists($parentId, $childrenReferences)) {
            $childrenReferences[$parentId] []= $id;
        } else {
            $childrenReferences[$parentId] = array($id);
        }
    
    }
    
    // output
    function out_nodes($nodes) {
        global $childrenReferences, $xmlNodes; // this is not required since php 5.3 or something about
        echo "<ul>";
        foreach ($nodes as $id) {
            $cat = $xmlNodes[$id];
            echo "<li>ID: " . $cat->Id . "--Name: " . $cat->Name;
            if (array_key_exists($id, $childrenReferences)) { // intermediate node
                out_nodes($childrenReferences[$id]);
            }
            echo "</li>";
        }
        echo "</ul>";
    }
    
    ob_start();
    out_nodes($rootNodes);
    ob_end_flush();
    

    Code may not work or even compile, but you get the idea.

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

Sidebar

Related Questions

I'm trying to read an object from a XML file using XMLDecoder. The construction
I am trying to read an XML-file from another server. However the the company
I have an xml file(from federal government's data.gov) which I'm trying to read with
I'm trying to read in some sample data from an XML file in a
I'm trying to read xml file from vbs script. Xml is encoded in utf-8
I'm currently trying to READ XML file from the client machine where the user
Hi I am trying to read an xml file from outside the web-app container,
I am trying to read an XML file using LINQ. I have had no
I am trying to read a xml file from the web and parse it
I am trying to read tags from XML using LibXML. I can print all

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.