dongzhan1948 2013-01-27 23:31
浏览 40
已采纳

什么是最好的PHP DOM 2阵列功能?

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条回答 默认 最新

  • dqyym3667 2013-01-28 01:47
    关注

    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'] ."
    ";
    // }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊