从 XML 对象中提取数据
我如何从该 xml 对象中提取数据,该对象是某个数组的值:
Array ( [Title] => SimpleXMLElement Object (
[0] => The Key of Life; A Metaphysical Investigation )
[ASIN] => SimpleXMLElement Object ( [0] => 0982385099 ) ...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对对象进行字符串类型转换。
$变量=(字符串)$FieldValue[0];
这可行,因为 SimpleXml 的所有子项都是对象类型而不是字符串。
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.
说 $X 是对象,所以要打印
:
say $X is the object, so to print
you do:
重要的是要明白,您使用的不是数组,而是使用 SimpleXMLElement 对象,这是不一样的。
您可以执行
$xml->tag->subtag
,而不是执行$array['key']['subkey']
。SimpleXML 节点不是字符串或数组,尽管它们的行为类似于字符串和数组。确保始终将值类型转换为显式字符串。
如果您要访问第一个节点,则无需使用
[0]
。这是假设的。您可以使用 PHP 5.2 或更高版本将 SimpleXMLElement 对象转换为真正的关联数组:
$array = json_decode(json_encode($xml), true);
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.
Instead of doing
$array['key']['subkey']
, you would do$xml->tag->subtag
.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.
If you're accessing the first node, you don't need to use
[0]
. It's assumed.You can convert SimpleXMLElement objects into true associative arrays in PHP 5.2 or newer with:
$array = json_decode(json_encode($xml), true);