[英]How parsing out XML file with namespaces?
I know similar questions were posted before, but I can't parse out this XML file with namespaces. 我知道以前曾发布过类似的问题,但是我无法使用名称空间解析该XML文件。
Here is the link to it because it's too big to post here: https://tsdrapi.uspto.gov/ts/cd/casestatus/sn86553893/info.xml 这是它的链接,因为它太大了,不能在此处发布: https : //tsdrapi.uspto.gov/ts/cd/casestatus/sn86553893/info.xml
I tried using simplexml_load_file
but that does not create xml object. 我尝试使用simplexml_load_file
但是没有创建xml对象。 Then I found similar problems and try something like this, provided I already downloaded file named it 86553893.xml 然后,我发现了类似的问题,并尝试了类似的方法,只要我已经下载了名为86553893.xml的文件
Here is my php code: 这是我的PHP代码:
$xml= new SimpleXMLElement("86553893.xml");
foreach($xml->xpath('//com:ApplicationNumber') as $event) {
var_export($event->xpath('com:ApplicationNumberText'));
}
You will have to register the namespaces on each element you want to use them: 您将必须在要使用它们的每个元素上注册名称空间:
$xml= new SimpleXMLElement("86553893.xml");
$xml->registerXpathNamespace('com', 'http://www.wipo.int/standards/XMLSchema/Common/1');
foreach ($xml->xpath('//com:ApplicationNumber') as $event) {
$event->registerXpathNamespace(
'com', 'http://www.wipo.int/standards/XMLSchema/Common/1'
);
var_export($event->xpath('com:ApplicationNumberText'));
}
This is different in DOM, you use an DOMXPath instance, so it is only a single object and you will have to register the namespaces only once. 这在DOM中是不同的,您使用DOMXPath实例,因此它只是一个对象,并且只需要注册一次名称空间。
$dom = new DOMDocument();
$dom->load("86553893.xml");
$xpath = new DOMXpath($dom);
$xpath->registerNamespace('com', 'http://www.wipo.int/standards/XMLSchema/Common/1');
foreach ($xpath->evaluate('//com:ApplicationNumber') as $event) {
var_export($xpath->evaluate('string(com:ApplicationNumberText)', $event));
}
You need pass the 3th param as true
: 您需要将第三个参数传递为true
:
<?php
$xml= new SimpleXMLElement("info.xml", NULL, true);
foreach($xml->xpath('//com:ApplicationNumber') as $event) {
var_export($event->xpath('com:ApplicationNumberText'));
}
Output: 输出:
array (
0 =>
SimpleXMLElement::__set_state(array(
)),
)
you can read more about SimpleXMLElement
in: 您可以在以下网站中了解有关SimpleXMLElement
更多信息:
http://php.net/manual/en/simplexmlelement.construct.php http://php.net/manual/en/simplexmlelement.construct.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.