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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:36:53+00:00 2026-06-13T21:36:53+00:00

I’m trying to build an unordered list from an existing XML file which contains

  • 0

I’m trying to build an unordered list from an existing XML file which contains categories and subcategories + images. I need each subcategory to be an item in an unordered list with each related image in a nested list. The subcategories should be shown only once (must not repeat if it’s the same). I believe what I’m looking for is a recursive loop, but that’s where I get lost.

Essentially, I’m trying to achieve a look similar to this:

————–DOGS————-
[……….small……….]
[picture] [picture]
[……….large……….]
[picture]
————–CATS————-
[……….medium……….]
[picture]
[……….large……….]
[picture] [picture]

My XML file structure:

 <root>
    <animal>
        <category>DOGS</category>
        <subcategory>small</subcategory>
        <name>Terrier</name>
        <image>aaa.jpg</image>
    </animal>
    <animal>
        <category>-</category>
        <subcategory>small</subcategory>
        <name>Havanese</name>
        <image>bbb.jpg</image>
    </animal>
    <animal>
        <category>-</category>
        <subcategory>large</subcategory>
        <name>Dalmatian</name>
        <image>ccc.jpg</image>
    </animal>
    <animal>
        <category>CATS</category>
        <subcategory>medium</subcategory>
        <name>Abyssinian</name>
        <image>ddd.jpg</image>
    </animal>
    <animal>
        <category>-</category>
        <subcategory>large</subcategory>
        <name>Birman</name>
        <image>eee.jpg</image>
    </animal>
    <animal>
        <category>-</category>
        <subcategory>large</subcategory>
        <name>American Shorthair</name>
        <image>fff.jpg</image>
    </animal>
</root>

Here’s what I have attempted so far:

    <?php

    $xml = simplexml_load_file("file.xml");

    $categories     = array();
    $subcategories = array();
    $names      = array();
    $image         = array();

    foreach($xml->animal as $animals) { 
        $category        = $animals->category;
        $subcategory     = $animals->subcategory;
        $name            = $animals->name;
        $image       = $animals->image;

        $categories[]     = $category;
        $subcategories[] = $subcategory;
        $names[]          = $name;
        $images[]        = $image
    }

    function getMenu($xml, $categories, $subcategories, $names, $images) {
        $output = '<ul>';
             foreach(array_keys($images) as $n) {       
                 $output .= '<li class="animals" data-tags="'.$names[$n].'">';
                 $output .= '<img src="xml/'.$images[$n].'" width="75" height="75" alt="'.$names[$n].'" />';
                 $output .= '</li>';    
             }

        $output.= '</ul>';  
        return array($output);  
    }

    $result = getMenu($xml, $categories, $subcategories, $names, $images);

    echo json_encode($result);

    ?>
  • 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-13T21:36:55+00:00Added an answer on June 13, 2026 at 9:36 pm

    There are literally a thousand ways how you can do that. Most straight forward is probably with xpath(). It’s a powerful XML querying language worth to learn. Example with your $xml simplexmlelement:

    foreach ($xml->xpath('//category[not(. = following::category)]') as $category) {
        echo "=== $category === \n";
        foreach ($xml->xpath("//animal[category = '$category']/subcategory[not(. = following::animal[category = '$category']/subcategory)]") as $subcategory) {
            echo "  = $subcategory =\n";
            foreach ($xml->xpath("//animal[category = '$category' and subcategory = '$subcategory']") as $animal) {
                echo "    * $animal->name ($animal->image)\n";
            }
        }
    }
    

    Output:

    === DOGS === 
      = small =
        * Terrier (aaa.jpg)
        * Havanese (bbb.jpg)
      = large =
        * Dalmatian (ccc.jpg)
    

    You further on might want to replace the lengthy xpath strings out there. With the help of a closure and an iterator aggregate, it’s even possible to reduce the code quite comfortable to:

    foreach ($categories as $category) {
        echo "=== $category === \n";
        foreach ($subcategories as $subcategory) {
            echo "  = $subcategory =\n";
            foreach ($animals as $animal) {
                echo "    * $animal->name ($animal->image)\n";
            }
        }
    }
    

    This would allow to change the XML structure later on and have the xpath’s configured in a central location:

    $categories    = $vpath('//category[not(. = following::category)]');
    $subcategories = $vpath('//animal[category = "%1$s"]/subcategory[not(. = following::animal[category = "%1$s"]/subcategory)]', [&$category]);
    $animals       = $vpath("//animal[category = '%s' and subcategory = '%s']", [&$category, &$subcategory]);
    

    I have put that example online as a demo.

    I have the output just as text with indents, however thanks to the foreachs it should be really straight forward to turn that into HTML. I leave that as an exercise.

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

Sidebar

Related Questions

In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I'm trying to select an H1 element which is the second-child in its group
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an XML file, the creators of it stuck in a bunch social
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has

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.