SimpleXML 和 print_r() - 为什么这是空的? [英] SimpleXML and print_r() - why is this empty?
问题描述
这是我的 xml 文件:(thefile)
Hi there here is my xml file: (thefile)
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://nts-de-osm1-pxc/webservices/">
<Item xmlns:q1="http://systinet.com/wsdl/com/osm/webservices/service/" xsi:type="q1:Document">
<q1:attributes>
<q1:Attribute>
<q1:dataDictionary xsi:nil="true" />
<q1:dataType>string</q1:dataType>
<q1:displayName>AEND_DATUM</q1:displayName>
<q1:key>false</q1:key>
<q1:name>AEND_DATUM</q1:name>
<q1:searchable>true</q1:searchable>
<q1:userAttribute>true</q1:userAttribute>
<q1:value>10.05.10</q1:value>
</q1:Attribute>
<q1:Attribute>
<q1:dataDictionary xsi:nil="true" />
<q1:dataType>string</q1:dataType>
<q1:displayName>AEND_PRUEF_DATUM</q1:displayName>
<q1:key>false</q1:key>
<q1:name>AEND_PRUEF_DATUM</q1:name>
<q1:searchable>true</q1:searchable>
<q1:userAttribute>true</q1:userAttribute>
<q1:value>10.05.10</q1:value>
</q1:Attribute>
</q1:attributes>
</Item>
</ArrayOfItem>
这是我的代码
$xml = simplexml_load_file($thefile);
print_r($xml);
这是输出
SimpleXMLElement Object
(
[Item] => SimpleXMLElement Object
(
)
)
为什么这是空的?
推荐答案
不要使用 print_r() 或 var_dump() 来检查 SimpleXMLElement,它们不一定能处理它们,因为 SimpleXML 在幕后使用了很多魔法.相反,看看 asXML()
返回什么.
Don't use print_r() or var_dump() to inspect a SimpleXMLElement, they won't necessarily work on them because SimpleXML uses lots of magic behind the scene. Instead, look at what asXML()
returns.
在您的情况下,它不会显示 <q1:attributes/>
因为它们不在同一个命名空间中.
In your case, it doesn't show <q1:attributes/>
because they're not in the same namespace.
要访问这些命名空间节点,有许多不同的方法,其中大部分在 Stack Overflow 上讨论过.如果您无法解决,请打开一个新问题,因为主题不同.以下是访问这些元素的 3 种方法:
To access those namespaced nodes, there are many different ways, most of them discussed here at Stack Overflow. If you can't work it out, please open a new question, since the subject is different. Here's 3 ways to access those elements:
$ArrayOfItem->Item->children("http://systinet.com/wsdl/com/osm/webservices/service/");
$ArrayOfItem->Item->children('q1', true);
$ArrayOfItem->Item->xpath('//q1:Attribute');
这篇关于SimpleXML 和 print_r() - 为什么这是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!