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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:31:53+00:00 2026-06-09T23:31:53+00:00

I am new to PHP so maybe this is silly question. How can I

  • 0

I am new to PHP so maybe this is silly question.
How can I easily manipulate with xml document using PHP and get data from it?
Can someone show me some example how it is done?

I tried this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note> 

And this is my php:

<?php
$xml = simplexml_load_file("test.xml");

echo $xml->getName() . "<br />";

foreach($xml->children() as $child)
{
 echo $child->getName() . ": " . $child . "<br />";
}
?> 

This works for only this document, but when i try some bigger xml it doesn’t work. It only writes back one tag all the time.

  • 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-09T23:31:54+00:00Added an answer on June 9, 2026 at 11:31 pm

    Your problem is that with your code you only fetch first nodes of xml documents. Try like this:

                    $xml = simplexml_load_file($url) ;
                    echo '<br /><b>all of the data:</b>'.'<br>  </br><div class="data">';
                    echo $xml->getName() . "<br />"; 
                    foreach($xml -> children() as $name => $child ){
                    echo $name.'<br />';
                        foreach($child -> children() as $name1 => $child1  ) {
                        echo $name1.'<br />'; //fetching children nodes
                            foreach($child1 -> children() as $name2 => $child2  ) {
                            echo $name2.'<br />';
                            }
                            // ..... and so on...depends how deep is you node tree
                        }
    
                    }
    

    you can use xpath to fetch what you want. It’s easy and you can build queries with it. Try like this:

                    $result=$xml->xpath($your_query);
                    foreach($result as $bla) {
                        echo "$bla<br />";
                        }
    

    You can use this CD catalog to test you code: http://www.w3schools.com/xml/cd_catalog.xml

    Here are some examples of using xpath: http://www.w3schools.com/xpath/xpath_examples.asp

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

Sidebar

Related Questions

This may be a silly question, but as someone relatively new to PHP, I'm
New to PHP and MySQL, have heard amazing things about this website from Leo
I am new to Java coming from a PHP background so sorry if this
Maybe this question is silly but I really don't know how to solve. First,
I'm kinda new to jQuery, so this may be a silly question... My problem
I'm new to zend framework so maybe this question is stupid.. I've got a
I'm new to PHP so maybe I am overlooking something here but the following:
Rather new to php, so sorry if this seems stupid. I'm really copying a
Im new to PHP and I can't figure out what the rules are for
I'm using the Facebook php-sdk to get the users name and friends, right now

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.