PHP-如何解析此xml? [英] PHP - How to parse this xml?

查看:67
本文介绍了PHP-如何解析此xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析下面的XML,以便最终得到一个类似于所包含示例的数组...我很难弄清楚如何获取标签内部的属性以输出我想要的方式...

I'm trying to parse the XML below so that I wind up with an array that looks like the sample included... I'm having a hard time figuring out how to get the attributes inside of the tags to output the way I want it to...

XML

<cust rid="999999" memberid="12345" lname="Doe" fname="John">
    <address memberid="12345" address1="1234 Mockingbird Lane" address2="" city="Oakland" state="CA" zip="91111" country="United States"/>
    <phone memberid="12345" phonenumber="415.555.1212" countrycodeid="1" phonetype="Mobile"/>
    <custcode memberid="12345" ccode="Silver" deleted="1"/>
    <custcode memberid="12345" ccode="Gold"/> 
</cust>

我想要的数组::注意添加的数组元素

Array
    [cust] => Array
        [rid] => 999999
        [member_id] => 12345
        [lname] => Doe
        [fname] => John
        [address] => Array
            [0] => Array
                [memberid] => 12345
                [address1] => 1234 Mockingbird Lane
                [address2] =>
                [city] => Oakland
                [state] => CA
                [zip] => 91111
                [country] => United States
        [phone] => Array
            [0] => Array
                [memberid] => 12345
                [phonenumber] => 415.555.1212
                [countrycodeid] => 1
                [phonetype] => Mobile
        [custcode] => Array
            [0] => Array
                [memberid] => 12345
                [ccode] => Silver
                [deleted] => 1
            [1] => Array
                [memberid] => 12345
                [ccode] => Gold

推荐答案

您可以使用DOMDocument及其字段/方法来提取数据.

You can use DOMDocument and its fields/methods to extract the data.

DOMDocument也是DOMNode,它具有一个称为attribute的字段,该字段是属性名称和值节点的映射,因此您可以从中获取属性名称和值(#6-9). 每个节点都具有firstChild和next兄弟,您可以浏览所有子节点(#12-26).

DOMDocument is also DOMNode has a field called attributes which is a map of attribute name and the node of value so you can get the attribute name and value from it (#6-9). Each node alos have firstChild and next sibling which you can navigate through all sub nodes (#12-26).

由此您可以相应地创建数据数组.这是我使用的代码(进行了一些调整).

From that you can create array of data accordingly. Here is the code I used (with a bit adjustments).

/*01*/ function Parse($XMLNode, $XMLData = null) {
/*02*/     if ($XMLData == null)
/*03*/     $XMLData = array();
/*04*/     
/*05*/     // Extract attribute as the array values
/*06*/     if ($XMLNode->hasAttributes()) {
/*07*/         foreach ($XMLNode->attributes as $AttrName => $AttrNode)
/*08*/             $XMLData[$AttrName] = $AttrNode->nodeValue;
/*09*/     }
/*10*/     
/*11*/     // Loop all child
/*12*/     $Child = $XMLNode->firstChild;
/*13*/     while ($Child != null) {
/*14*/         $Name = $Child->nodeName;
/*15*/         if ($Name != "#text") { // Ignore the text
/*16*/             $Nodes = $XMLData[$Name];
/*17*/             if ($Nodes == null)
/*18*/                 $Nodes = array();   // Array value from subnode is a nest array
/*19*/             
/*21*/             $Nodes[] = Parse($Child);
/*22*/             $XMLData[$Name] = $Nodes;
/*23*/         }
/*24*/         
/*25*/         $Child = $Child->nextSibling;
/*26*/     }
/*27*/     return $XMLData;
/*28*/ }
/*29*/ 
/*31*/ $XML = <<<EOF
/*32*/ <cust rid="999999" memberid="12345" lname="Doe" fname="John">
/*33*/     <address memberid="12345" address1="1234 Mockingbird Lane" address2="" city="Oakland" state="CA" zip="91111" country="United States" />
/*34*/     <phone memberid="12345" phonenumber="415.555.1212" countrycodeid="1" phonetype="Mobile" />
/*35*/     <custcode memberid="12345" ccode="Silver" deleted="1" />
/*36*/     <custcode memberid="12345" ccode="Gold" /> 
/*37*/ </cust>
/*38*/ EOF;
/*39*/ 
/*41*/ $DOC = new DOMDocument();
/*42*/ $DOC->loadXML($XML);
/*43*/ $Data = Parse($DOC->firstChild);
/*44*/ 
/*45*/ print_r($Data);

上面的代码按预期返回结果.

The above code returns the result as you expected.

希望这会有所帮助.

这篇关于PHP-如何解析此xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆