Extract data from an XML object

发布于 2022-09-06 08:35:58 字数 290 浏览 13 评论 0

How do i extract the data from that xml object which is a value of a certain array:

Array ( [Title] => SimpleXMLElement Object ( 
[0] => The Key of Life; A Metaphysical Investigation ) 
[ASIN] => SimpleXMLElement Object ( [0] => 0982385099 ) ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长伴 2022-09-13 08:35:58

Do string typecasting of the object.

$variable = (string) $FieldValue[0];

That would work, as SimpleXml has all the children in object type and not string.

沉睡月亮 2022-09-13 08:35:58

say $X is the object, so to print

The Key of Life; A Metaphysical
Investigation

you do:

echo $X->Title[0]
短暂陪伴 2022-09-13 08:35:58

It's important to understand that you're not working with an array — you're working with a SimpleXMLElement object, which is not the same.

  1. Instead of doing $array['key']['subkey'], you would do $xml->tag->subtag.

  2. SimpleXML nodes are not strings or arrays, although they behave string-like and array-like. Make sure you always typecast the value to an explicit string.

  3. If you're accessing the first node, you don't need to use [0]. It's assumed.

  4. You can convert SimpleXMLElement objects into true associative arrays in PHP 5.2 or newer with:

    $array = json_decode(json_encode($xml), true);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文