从SimpleXMLElement对象访问单个属性

时间:2014-11-07 14:52:38

标签: php object simplexml

在将此标记为重复之前,请注意我找不到具体案例的答案。

我有一个SOAP XML响应,我使用:

存储在一个对象中

$ resultObj = SimpleXML_Load_String($ xml);

如果我print_r对象,我得到:

SimpleXMLElement Object
(
    [soap_Body] => SimpleXMLElement Object
        (
            [SubmitNewApplicationShortResponse] => SimpleXMLElement Object
                (
                    [SubmitNewApplicationShortResult] => SimpleXMLElement Object
                        (
                            [Errors] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [IsError] => true
                                        )

                                    [ErrorDetails] => SimpleXMLElement Object
                                        (
                                            [ErrorDetail] => SimpleXMLElement Object
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [Code] => 209
                                                            [Message] => Client Reference already exists.
                                                        )

                                                )

                                        )

                                )

                            [Token] => 00000000-0000-0000-0000-000000000000
                            [ProposalID] => 0
                        )

                )

        )

)

我可以使用以下方式访问令牌:

$token  = (string)$resultObj->soap_Body->SubmitNewApplicationShortResponse->SubmitNewApplicationShortResult->Token

但是,我不知道如何访问Code和Message属性。 什么是“@attributes”?

编辑:似乎我的代码中有错误,在SimpleXML对象中应忽略@attributes。 这有效,如@ mark91所示:

print_r( (string) $resultObj->soap_Body->SubmitNewApplicationShortResponse->SubmitNewApplicationShortResult->Errors->ErrorDetails->ErrorDetail["Code"] );

1 个答案:

答案 0 :(得分:1)

你试过这个吗?

 $resultObj->SubmitNewApplicationResponse->SubmitNewApplicationResult->Errors->ErrorDetails->ErrorDetail["Code"];