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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:18:04+00:00 2026-06-18T00:18:04+00:00

I want to parse xml files, The best way I found is to use

  • 0

I want to parse xml files,
The best way I found is to use DOMDocument() class so far.

sample xml string:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<response>
<resData>
<contact:infData xmlns:contact="http://example.com/contact-1.0">
<contact:status s="value1"/>
<contact:status s="value2"/>
<contact:status s="value3"/>
<contact:status s="value4"/>
</contact:infData>
</resData>
</response>

I use function dom2array (bellow) to parse dom, but it only return 1 element (value4 only)

<?php
    function dom2array($node) {
    $res = array();
    if($node->nodeType == XML_TEXT_NODE){
       $res = $node->nodeValue;
    }else{
       if($node->hasAttributes()){
           $attributes = $node->attributes;
            if(!is_null($attributes)){
               $res['@attributes'] = array();
               foreach ($attributes as $index=>$attr) {
                   $res['@attributes'][$attr->name] = $attr->value;
               }
           }
       }
       if($node->hasChildNodes()){
           $children = $node->childNodes;
           for($i=0;$i<$children->length;$i++){
               $child = $children->item($i);
               $res[$child->nodeName] = dom2array($child);
           }
       }
    }
    return $res;
    }
?>

Is there any way to parse all xml elements and sent them to an array?

output array:

Array
(
[response] => Array
    (
        [#text] => 

        [resData] => Array
            (
                [#text] => 

                [contact:infData] => Array
                    (
                        [#text] => 

                        [contact:status] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [s] => value4
                                    )

                            )

                    )

            )

    )

)

Where is value1, value2, value3 ? 🙁
Thank you

  • 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-18T00:18:05+00:00Added an answer on June 18, 2026 at 12:18 am

    You can use this (based on: http://php.net/manual/en/book.dom.php#93717);

    function xml_to_array($root) {
        $result = array();
    
        if ($root->hasAttributes()) {
            $attrs = $root->attributes;
            foreach ($attrs as $attr) {
                $result['@attributes'][$attr->name] = $attr->value;
            }
        }
    
        if ($root->hasChildNodes()) {
            $children = $root->childNodes;
            if ($children->length == 1) {
                $child = $children->item(0);
                if ($child->nodeType == XML_TEXT_NODE) {
                    $result['_value'] = $child->nodeValue;
                    return count($result) == 1
                        ? $result['_value']
                        : $result;
                }
            }
            $groups = array();
            foreach ($children as $child) {
                if (!isset($result[$child->nodeName])) {
                    $result[$child->nodeName] = xml_to_array($child);
                } else {
                    if (!isset($groups[$child->nodeName])) {
                        $result[$child->nodeName] = array($result[$child->nodeName]);
                        $groups[$child->nodeName] = 1;
                    }
                    $result[$child->nodeName][] = xml_to_array($child);
                }
            }
        }
    
        return $result;
    }
    

    Test;

    $s = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
            <response>
                <resData foo="1">
                    <contact:infData xmlns:contact="http://example.com/contact-1.0" bar="1">
                        <contact:status s="value1"/>
                        <contact:status s="value2"/>
                        <contact:status s="value3"/>
                        <contact:status s="value4"/>
                    </contact:infData>
                </resData>
            </response>';
    
    $xml = new DOMDocument();
    $xml->loadXML($s);
    $xmlArray = xml_to_array($xml);
    print_r($xmlArray);
    // print_r($xmlArray['response']['resData']['contact:infData']['contact:status'][0]['@attributes']['s']);
    // foreach ($xmlArray['response']['resData']['contact:infData']['contact:status'] as $status) {
        // echo $status['@attributes']['s'] ."\n";
    // }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse xml files that have elements like these: <element>&amp</element> <element>&amp;</element> But
I want to parse a couple of thousands XML-files from a website(I have permission)
I want to parse a xml file using xquery in Ruby, I found this
I have several large .xml files. I want to parse out the files to
I've got an XML file I want to parse with python. What is best
I have 200,000 XML files I want to parse and store in a database.
I want to parse xml file in utf-8 and sort it by some field.
I want to parse the xml file with dynamic content using DOM parser in
I want to parse a large XML file and I have two options: Perl
I have an XML file and parse it into my PHP document. I want

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.