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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:15:01+00:00 2026-05-16T05:15:01+00:00

I’m trying to parse a quite large XML-sheet with PHP, but I’m fairly new

  • 0

I’m trying to parse a quite large XML-sheet with PHP, but I’m fairly new to it.
The XML-sheet contains a couple of thousands of records.

Here is an example of the structure used within the sheet:

<familyList>
<family>
<familyID>1234</familyID>
<familyDescription>The Jonathans</familyDescription>
<childrenList>
<child>Suzan</child>
<child>Fred</child>
<child>Harry</child>
</childrenList>
</family>
<family>
<familyID>1235</familyID>
<familyDescription>The Gregories</familyDescription>
<childrenList>
<child>Anthony</child>
<child>Lindsay</child>
</childrenList>
</family>
</familyList>

As I’m fairly new to XML-parsing using PHP, I wonder what would be the best way to parse this nested XML-sheet into an array. I need to convert the XML to an array so I can insert the data into a MySQL database afterwards.

Could you please give me a push in the right direction as I haven’t been succesful puzzling out a solution sofar?..

Thanks!

  • 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-16T05:15:01+00:00Added an answer on May 16, 2026 at 5:15 am

    DOMDocument has lots of excellent methods for accessing, updating and outputting formatted XML. With regards to converting to an array, I’d suggest going for objects inside an array, which is something that PHP is perfectly fine with, and I find the syntax much clearer than arrays for keeping track of this kind of hierarchy.

     <?php
    
    
    // load xml families, could split this into different files..
    $families = new DOMDocument();
    $families->load("/xml/families.xml"); // your xml file
    
    $families_items = $families->getElementsByTagName("family");
    
    $my_cool_array = null;  // reset this variable for being set as an array literal later
    
    foreach( $families_items as $family_item) {
    
        $toinsert = null; // reset the object literal
    
        $toinsert->family_id = $family_item->getElementsByTagName('familyID')->nodeValue;
        $toinsert->familyDescription= $family_item->getElementsByTagName('familyDescription')->nodeValue;
    
        $children = $family_item->getElementsByTagName('childrenList')->childNodes;
    
    
        // children
        foreach ($children as $child) {
            $child_toinsert[]->name = $child->nodeValue;
        }
        // etc for your details, syntax might be a bit off, but should get you started
    
        $toinsert->children = $child_toinsert;
    
    
        // build array of objects
        $my_cool_array_of_families[] = $toinsert;
    
    
    
    }
    
    
    var_dump($my_cool_array);
    

    Something like this, double check the syntax, but it’s on the way 😉

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

Sidebar

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.