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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:52:48+00:00 2026-06-13T14:52:48+00:00

I need help to parse XML with many level nodes into PHP as follows:

  • 0

I need help to parse XML with many level nodes into PHP as follows:

catalog.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<Catalog>
<Category>
<Name>Biscuit</Name>
<Type>
<Type1>Chocolate Chips</Type1>
<Ingredient>Flour, Chocolate Chips</Ingredient>
<Duration>15 minute Oven Bake</Duration>
</Type>
<Type>
<Type2>Lemon Puff</Type2>
<Ingredient>Flour, Lemon Extract</Ingredient>
<Duration>15 minute Oven Bake</Duration>
</Type>
</Category>
<Category>
<Name>Cake</Name>
<Type>
<Type1>Fruit Cake</Type1>
<Ingredient>Flour, Egg, Fresh Cream, Fruits</Ingredient>
<Duration>30 minute Conventional Oven Bake</Duration>
</Type>
<Type>
<Type2>Mango Cake</Type2>
<Ingredient>Flour, Egg, Fresh Cream, Mango, Nuts</Ingredient>
<Duration>30 minute Oven Bake</Duration>
</Type>
</Category>
</Catalog>

menu.php

<?php
$xml = simplexml_load_file("catalog.xml");
foreach ($xml->xpath('//Category') as $category)
{
echo $category->Name;
foreach($xml->xpath('//Site') as $site)
{
echo $site->ID;
}
}
?>

Something is not right in my php which I tried to solve going through examples but still having problem. Sorry this is my first php coding project. Thanks in advance to many experts.

  • 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-13T14:52:49+00:00Added an answer on June 13, 2026 at 2:52 pm

    Please have a read of http://devzone.zend.com/240/simplexml/
    example:

    <?php
    // $xml = simplexml_load_file("catalog.xml");
    $catalog = new SimpleXMLElement(data());
    
    foreach ( $catalog->Category as $category ) {
        echo 'Name: ', $category->Name, "\n";
        foreach( $category->Type as $type ) {
            if ( isset($type->Type1) ) {
                echo "  type1: ", $type->Type1, "\n";
            }
            else if ( isset($type->Type2) ) {
                echo "  type2: ", $type->Type2, "\n";
            }
            else {
                echo "  unknown type\n";
            }
            echo '    Ingredient:', $type->Ingredient, "\n";
            echo '    Duration: ', $type->Duration, "\n";
        }
    }
    
    
    function data() {
        return <<< eox
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <Catalog>
    <Category>
    <Name>Biscuit</Name>
    <Type>
    <Type1>Chocolate Chips</Type1>
    <Ingredient>Flour, Chocolate Chips</Ingredient>
    <Duration>15 minute Oven Bake</Duration>
    </Type>
    <Type>
    <Type2>Lemon Puff</Type2>
    <Ingredient>Flour, Lemon Extract</Ingredient>
    <Duration>15 minute Oven Bake</Duration>
    </Type>
    </Category>
    <Category>
    <Name>Cake</Name>
    <Type>
    <Type1>Fruit Cake</Type1>
    <Ingredient>Flour, Egg, Fresh Cream, Fruits</Ingredient>
    <Duration>30 minute Conventional Oven Bake</Duration>
    </Type>
    <Type>
    <Type2>Mango Cake</Type2>
    <Ingredient>Flour, Egg, Fresh Cream, Mango, Nuts</Ingredient>
    <Duration>30 minute Oven Bake</Duration>
    </Type>
    </Category>
    </Catalog>
    eox;
    }
    

    prints

    Name: Biscuit
      type1: Chocolate Chips
        Ingredient:Flour, Chocolate Chips
        Duration: 15 minute Oven Bake
      type2: Lemon Puff
        Ingredient:Flour, Lemon Extract
        Duration: 15 minute Oven Bake
    Name: Cake
      type1: Fruit Cake
        Ingredient:Flour, Egg, Fresh Cream, Fruits
        Duration: 30 minute Conventional Oven Bake
      type2: Mango Cake
        Ingredient:Flour, Egg, Fresh Cream, Mango, Nuts
        Duration: 30 minute Oven Bake
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help with the following issue. I parse XML and do a XSLT
I need to parse XML string into an array. I have XML <group xmlns=123
I need to parse an xml string and find values of specific text nodes,
Hello i need parse this xml with C# in console app , any help
I need help with downloading from webserver... What i currently do is get XML
I need to parse DTDs using PHP and am hoping there's a simple library
I need to a parse an xml string(.NET, C#) which , unfortunately, is not
I am using REST web service and i need to parse the XML that
I have an application where I need to parse or tokenize XML and preserve
I'm looking for an efficient and reusable way to parse xml into an object

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.