如何将此XML解析为PHP

时间:2012-10-16 18:47:37

标签: php xml xml-parsing

Hiyo,

我经常使用XML和PHP,但我得到的具体回报是如何在PHP中获取它。如果我能将“$ attribute”和“VALUE”转换为一个关联数组,我会很乐意。

真的很难过和沮丧 射线

SimpleXMLElement Object
(
[MULTIPLE] => SimpleXMLElement Object
    (
        [SINGLE] => SimpleXMLElement Object
            (
                [KEY] => Array
                    (
                        [0] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [name] => address
                                    )

                                [VALUE] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [null] => null
                                            )

                                    )

                            )

                        [1] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [name] => aim
                                    )

                                [VALUE] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [null] => null
                                            )

                                    )

                            )

                        [2] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [name] => auth
                                    )

                                [VALUE] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [null] => null
                                            )

                                    )

                            )

更新: 这是XML输出:

<?xml version="1.0" encoding="UTF-8" ?>
<RESPONSE>
<MULTIPLE>
<SINGLE>
<KEY name="address"><VALUE null="null"/>
</KEY>
<KEY name="aim"><VALUE null="null"/>
</KEY>
<KEY name="auth"><VALUE null="null"/>
</KEY>
<KEY name="city"><VALUE null="null"/>
</KEY>
<KEY name="confirmed"><VALUE null="null"/>
</KEY>
<KEY name="country"><VALUE null="null"/>
</KEY>
<KEY name="deleted"><VALUE null="null"/>
</KEY>
<KEY name="department"><VALUE null="null"/>
</KEY>
<KEY name="description"><VALUE null="null"/>
</KEY>
<KEY name="email"><VALUE null="null"/>
</KEY>
<KEY name="emailstop"><VALUE null="null"/>
</KEY>
<KEY name="error"><VALUE>no match found for idnumber = 301942</VALUE>
</KEY>
<KEY name="firstname"><VALUE null="null"/>
...

我知道如何在以该教程中描述的方式形成XML时解析XML。但是这个具体案例让我难以理解如何访问我需要的节点(通过SimpleXML对象或将其转换为PHP数组而不通过这样的节点:

$xml_obj->MULTIPLE->SINGLE->KEY[9]->VALUE

1 个答案:

答案 0 :(得分:0)

this一个阅读,它会给你一个简短的教程