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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:38:12+00:00 2026-05-14T03:38:12+00:00

Introduction: I want to loop through XML files with flexible categories structure. Problem: I

  • 0

Introduction:

I want to loop through XML files with flexible categories structure.

Problem:

I don’t know to loop through a theoretical infinte subcategories without having to make x amount of “for each” statements (See coding example in the bottom). How do I dynamically traverse the categories structure?

<?xml version="1.0" encoding="utf-8"?>
<catalog>
    <category name="Category - level 1">
        <category name="Category - level 2" />
        <category name="Category - level 2">
            <category name="Category - level 3" />
        </category>
        <category name="Category - level 2">
            <category name="Category - level 3">
                <category name="Category - level 4" />
            </category>
        </category>
    </category>
</catalog>

What I have now:

I have no problem looping through XML files with a set structure:

<catalog>
    <category name="Category - level 1">
        <category name="Category - level 2">
            <category name="Category - level 3" />
        </category>
        <category name="Category - level 2">
            <category name="Category - level 3" />
        </category>
    </category>
</catalog>

Coding example:

//$xml holds the XML file
foreach ( $xml AS $category_level1 )
{
    echo $category_level1['name'];

    foreach ( $category_level1->category AS $category_level2 )
    {
        echo $category_level2['name'];

        foreach ( $category_level2->category AS $category_level3 )
        {
           echo $category_level3['name'];
        }
    }
}
  • 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-14T03:38:12+00:00Added an answer on May 14, 2026 at 3:38 am

    Getting the name attributes from your categories is likely fastest when done via XPath, e.g.

    $categoryNames = $doc->xpath('//category/@name');
    

    However, if you want to recursively iterate over an arbitrary nested XML structure, you can also use the SimpleXMLIterator, e.g. with $xml being the string you gave:

    $sxi = new RecursiveIteratorIterator(
               new SimpleXMLIterator($xml), 
               RecursiveIteratorIterator::SELF_FIRST);
    
    foreach($sxi as $node) {
        echo str_repeat("\t", $sxi->getDepth()), // indenting
             $node['name'],                      // getting attribute name
             PHP_EOL;                            // line break
    }
    

    will give

    Category - level 1
        Category - level 2
        Category - level 2
            Category - level 3
        Category - level 2
            Category - level 3
                Category - level 4
    

    Like said in the beginning, when just wanting to get all name attributes, use XPath, because iterating over each and every node is slow. Use this approach only when you want to do more complex things with the nodes, for instance adding something to them.

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

Sidebar

Related Questions

I'm reading through A Gentle Introduction to Haskell, and early on it uses this
Introduction I've always been searching for a way to make Visual Studio draw a
Introduction In my current organisation, we have many desktop and web applications all feeding
Has the introduction of the .net framework made raw programming in COM and DCOM
Can anyone point me to a good introduction to coding against the paypal API?
I'm working on getting an Introduction to Groovy presentation ready for my local Java
I am looking for some material giving introduction to DAO and ADO usage in
I'm looking for a laymen's introduction to computer hardware and organization. Here are some
I have a section: \section{Introduction} \label{sec:introduction} I'd like a link to the section where
I'm looking for a good 10 minute introduction to Unobtrusive Javascript using JQuery. I'm

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.