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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:54:25+00:00 2026-05-13T16:54:25+00:00

I have a basic XML file that I load in my PHP using $xml

  • 0

I have a basic XML file that I load in my PHP using

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

I want to be able to access my data using syntax something similar to:

$xml[0]['from'];
$xml['note']['from'];
$xml['from']['email'];

I know I can access the data using this:

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

However, this is NOT a preferred method, and I would like to use syntax similar to multidimensional arrays.

Using APIs from many different websites, this is generally how I would access XML, however, I can’t seem to do it with my own using simplexml_load_file("file.xml");

I know this is very simple but is there something I’m missing? Please help! Thanks in advance

XML File:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>
    <name>Jani</name>
    <email>email@email.com</email>
</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
  • 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-13T16:54:25+00:00Added an answer on May 13, 2026 at 4:54 pm

    i think its not a perfect solution for you but it might help you for your problem.

    XML FILE:

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

    PHP FILE:

    <?php 
        $xml = simplexml_load_file("new.xml");  
    
        $webroot="http://".$_SERVER['HTTP_HOST']."/";
        $doc = new DOMDocument();
    
        $doc->load( $webroot.'/new.xml' );
    
        $note = $doc->getElementsByTagName( "note" );
        $final_arr = array();
        $i=0;
        foreach($note as $note)
        {
            $to = $note->getElementsByTagName( "to" );
            $to = $to->item(0)->nodeValue;
            $final_arr[$i]['to'] = $to;
    
            $from = $note->getElementsByTagName( "from" );
            foreach($from as $from)
            {
                $name = $from->getElementsByTagName( "name" );
                $name = $name->item(0)->nodeValue;
                $final_arr[$i]['from']['name'] = $name;
                $email = $from->getElementsByTagName( "email" );
                $email = $email->item(0)->nodeValue;
                $final_arr[$i]['from']['email'] = $email;
            }
    
    
            $heading = $note->getElementsByTagName( "heading" );
            $heading = $heading->item(0)->nodeValue;
            $final_arr[$i]['heading'] = $heading;
    
            $body = $note->getElementsByTagName( "body" );
            $body = $body->item(0)->nodeValue;
            $final_arr[$i]['body'] = $body;
            $i++;
        }
    
        echo "<pre>";
        print_r($final_arr);
        echo "</pre>";
    ?>
    

    hope this works for you.

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

Sidebar

Related Questions

My knowledge of xml and php is somewhat basic but have been able to
Aloha, I have a 8MB XML file that I wish to deserialize. I'm using
I have a basic xml file that looks like this. <root> <item> <title><p>some title</p></title>
I have a xml file. That I want to parse with NXXMLParser. I have
I have a basic XML object that I'm working with. I can't figure out
I have a basic PHP form (a few fields and 2 checkboxes, I want
I have a problem. I have an XML file that contains information about 100
I have a very large XML file that I need to parse so I
So I have an XML file that looks a little like this <xml> <post>
I have a very short (so far), very basic XML file called config.xml like

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.