Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
715 views
in Technique[技术] by (71.8m points)

xml - What is the best php DOM 2 Array function?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'] ."
";
// }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...