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 574907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:52:53+00:00 2026-05-13T13:52:53+00:00

I have a function which uses simplexml to return the first level of nodes

  • 0

I have a function which uses simplexml to return the first level of nodes in an XML file and write it into an unordered list:

function printAssetMap() {
    $xml = simplexml_load_file(X_ASSETS);
    $assets = $xml->asset;
    $html = '<ul>'."\n";
    foreach ($assets as $asset) {
        $html .= '<li id="asset'.$asset->asset_assetid.'"><ins>&nbsp;</ins><a href="#">'.$asset->asset_name.' ['.$asset->asset_assetid.']</a></li>'."\n";
    }//end foreach
    $html .= '</ul>'."\n";
    return $html;
}// printAssetMap()

XML I am using, that has nested nodes:

<?xml version="1.0"?>
<assets>
  <asset>
    <asset_name>Home</asset_name>
    <asset_url>/home</asset_url>
    <asset_assetid>1</asset_assetid>
  </asset>
  <asset>
    <asset_name>Projects</asset_name>
    <asset_url>/projects</asset_url>
    <asset_assetid>2</asset_assetid>
    <asset>
      <asset_name>Portfolio</asset_name>
      <asset_url>/projects/portfolio</asset_url>
      <asset_assetid>3</asset_assetid>
    </asset>
    <asset>
      <asset_name>Current Jobs</asset_name>
      <asset_url>/projects/current-jobs</asset_url>
      <asset_assetid>4</asset_assetid>
    </asset>
  </asset>
</assets>

Now, I am starting to add child nodes of the nodes that I am currently returning. Is there a way to loop through ALL levels of child nodes in an xml file, even if I don’t know how many levels there are, and add those to my list?

  • 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-13T13:52:53+00:00Added an answer on May 13, 2026 at 1:52 pm

    So basically what you need to do is a function that takes each <asset/> child of current node, builds the HTML then checks if the current node has <asset/> children of its own and keeps recursing deeper down the tree.

    Here’s how you can do it:

    function printAssetMap()
    {
        return printAssets(simplexml_load_file(X_ASSETS));
    }
    
    function printAssets(SimpleXMLElement $parent)
    {
        $html = "<ul>\n";
        foreach ($parent->asset as $asset)
        {
            $html .= printAsset($asset);
        }
        $html .= "</ul>\n";
    
        return $html;
    }
    
    function printAsset(SimpleXMLElement $asset)
    {
        $html = '<li id="asset'.$asset->asset_assetid.'"><ins>&nbsp;</ins><a href="#">'.$asset->asset_name.' ['.$asset->asset_assetid.']</a>';
    
        if (isset($asset->asset))
        {
            // has <asset/> children
            $html .= printAssets($asset);
        }
    
        $html .= "</li>\n";
    
        return $html;
    }
    

    By the way, I would expect a function named “printX” to actually print or echo something, rather than return it. Perhaps you should name those functions “buildX” ?

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

Sidebar

Ask A Question

Stats

  • Questions 492k
  • Answers 492k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer While there is no only right way, the usual approach… May 16, 2026 at 10:27 am
  • Editorial Team
    Editorial Team added an answer Standard specifies that the result is always integer value equals… May 16, 2026 at 10:27 am
  • Editorial Team
    Editorial Team added an answer Probably the best way would involve a simple loop through… May 16, 2026 at 10:27 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a custom allocator function which uses sbrk() to obtain memory. How do
I have a large C++ function which uses OpenCV library and running on Windows
I have written a small function, which uses ElementTree and xpath to extract the
In my script, I have a function foo which basically uses pynotify to notify
I have a site which uses a lot of JavaScript (mainly jQuery) and I
I have my own implementation of smart pointer which uses reference counting as ownership
I am writing an Ajax application which uses a callback function to add a
I need to let the user specify a custom format for a function which
I'm working at a web site search which uses Sql server 2008 express edition.
I am writing an app which uses a timer to display a countdown on

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.