更改对数组或对象的xml响应

时间:2015-06-26 17:21:32

标签: php xml soap

我通过soap api获得xml响应

$data = <users><user id="1028240" enabled="True" mustChangePassword="False">
<username><![CDATA[Priye1]]></username>
<firstName><![CDATA[Priye]]></firstName>
<middleName></middleName>
<lastName><![CDATA[Priye]]></lastName>
<email></email>
<company></company>
</users>

当我通过

将$ data转换为简单的xml对象时
$array = simplexml_load_string($data);

我有对象,但对象不包含任何值。

转换对象后如下

[users] =&gt;排列             (                 [user] =&gt;排列                     (                         [0] =&gt;排列                             (                                 [@attributes] =&gt;排列                                     (                                         [id] =&gt; 1028240                                         [enabled] =&gt;真正                                         [mustChangePassword] =&gt;假                                     )

                            [username] => Array
                                (
                                )

                            [firstName] => Array
                                (
                                )

                            [middleName] => Array
                                (
                                )

                            [lastName] => Array
                                (
                                )

                            [email] => Array
                                (
                                )

                            [company] => Array
                                (
                                )

                            [address] => Array
                                (
                                )

                            [city] => Array
                                (
                                )

                            [province] => Array
                                (
                                )
}

}

1 个答案:

答案 0 :(得分:2)

您的xml输入似乎无效。在这种情况下,如果您没有进行任何错误检查,并且没有查看显示错误的日志文件,那么您可能很难看到...

以下是已修复内容的示例:

<?php

$data  = <<<EOT
<users>
  <user id="1028240" enabled="True" mustChangePassword="False">
    <username><![CDATA[Priye1]]></username>
    <firstName><![CDATA[Priye]]></firstName>
    <middleName></middleName>
    <lastName><![CDATA[Priye]]></lastName>
    <email></email>
    <company></company>
  </user>
</users>
EOT;

print_r(simplexml_load_string($data, null, LIBXML_NOCDATA));

请注意添加的</user>朝向结尾处关闭第二行中打开的user标记。如果没有该修复,您将在CLI或日志错误日志文件中收到此警告:PHP Warning: simplexml_load_string(): Entity: line 9: parser error : expected '>'

如果您愿意,还必须告诉xml解析器应该访问并解释包含的CDATA内容。这是因为这些数据并不真正被认为是xml文档本身的一部分。您可以使用最终LIBXML_NOCDATA调用中显示的标记print_r()来执行此操作。

该脚本的输出是:

SimpleXMLElement Object
(
    [user] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [id] => 1028240
                [enabled] => True
                [mustChangePassword] => False
            )

        [username] => Priye1
        [firstName] => Priye
        [middleName] => SimpleXMLElement Object
            (
            )

        [lastName] => Priye
        [email] => SimpleXMLElement Object
            (
            )

        [company] => SimpleXMLElement Object
            (
            )

    )

)