【问题标题】:Accesing individual property from SimpleXMLElement Object从 SimpleXMLElement 对象访问单个属性
【发布时间】:2015-01-04 09:36:00
【问题描述】:

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

我有一个 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”是什么?

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

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

【问题讨论】:

    标签: php object simplexml


    【解决方案1】:

    您是否尝试过这样做?

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

    【讨论】:

    • 你是对的。抱歉,我在处理和粘贴您的代码时犯了一个错误:soap_Body 错误,所以我正确更新了我的答案。现在它应该可以工作了。
    • 不,不行,为什么拿出soap_Body还能行?我已经提到过使用该部分我确实设法显示 Token 属性。
    • 嗯,这对我来说似乎很奇怪。访问属性的方法是我向您展示的方法。请问可以print_r($resultObj->SubmitNewApplicationResponse->SubmitNewApplicationResult->Errors->ErrorDetails)吗?
    • 毫无疑问需要soap_Body。感谢您的帮助,但在这种情况下,您的代码中有错误。看看我的问题,我明确指出我使用 $token = (string)$resultObj->soap_Body->SubmitNewApplicationShortResponse->SubmitNewApplicationShortResult->Token 成功获取了 Token 值
    • 是的,对不起,我忘了,我是说:print_r($resultObj->soap_Body->SubmitNewApplicationResponse->SubmitNewApplicationResult->Errors->ErrorDetails);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    相关资源
    最近更新 更多