我有这个xml文件,并想使用PHP获得d:DescriptionNL字段,只有我似乎找不到一种方法来获得它。我已经得到了标题,总结,并得到了$entry->content->{'m:properties'}
,这给了我一个对象作为回报。
这里有人知道这是正确的方式去还是如果我完全走错了路吗?
谢谢!
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://website.com" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">DigestedCatalogItems</title>
<id>http://website.com</id>
<updated>2014-07-09T12:55:29Z</updated>
<link rel="self" title="DigestedCatalogItems" href="DigestedCatalogItems" />
<entry>
<id>http://website.com</id>
<title type="text">Item Title</title>
<summary type="html"></summary>
<updated>2014-07-09T12:55:29Z</updated>
<author>
<name>Item</name>
</author>
<contributor>
<uri>http://website.com</uri>
</contributor>
<link rel="edit" title="DigestedCatalogItems" href="DigestedCatalogItems('Car%20Plan')" />
<category term="IBP.Catalog.Web.DigestedCatalogItems" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:DescriptionNL>Item description</d:DescriptionNL>
<d:origin>Company</d:origin>
</m:properties>
</content>
</entry>
</feed>
这是我目前为止的PHP代码:
$response_xml_data = file_get_contents("http://website.com");
$data = simplexml_load_string($response_xml_data);
$array = $data->entry;
foreach($array as $row) {
//This get's me an stdClass object which I'm unable to explore any further
echo $row->content->{'m:properties'};
echo $row->author->name.' <br> ' . $row->content . '<br> <a target="_blank" href="'.$row->contributor->uri.'">'.$row->title.'</a>';
echo '<br><br>';
}
是的,你做错了。如果有名称空间,则必须使用 ->children()
方法并添加前缀。考虑这个例子:
$response_xml_data = <<<XML
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://website.com" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">DigestedCatalogItems</title>
<id>http://website.com</id>
<updated>2014-07-09T12:55:29Z</updated>
<link rel="self" title="DigestedCatalogItems" href="DigestedCatalogItems" />
<entry>
<id>http://website.com</id>
<title type="text">Item Title</title>
<summary type="html"></summary>
<updated>2014-07-09T12:55:29Z</updated>
<author>
<name>Item</name>
</author>
<contributor>
<uri>http://website.com</uri>
</contributor>
<link rel="edit" title="DigestedCatalogItems" href="DigestedCatalogItems('Car%20Plan')" />
<category term="IBP.Catalog.Web.DigestedCatalogItems" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:DescriptionNL>Item description</d:DescriptionNL>
<d:origin>Company</d:origin>
</m:properties>
</content>
</entry>
</feed>
XML;
$data = simplexml_load_string($response_xml_data);
$author = $data->entry->author->name;
$contributor = $data->entry->contributor->uri;
$title = $data->entry->title;
// content children has namespaces
$content = $data->entry->content->children('m', true)->children('d', true);
$description = (string) $content->DescriptionNL;
$origin = (string) $content->origin;
echo $author . '<br/>';
echo $description . '<br/>';
echo $origin . '<br/>';
echo '<br/>';
echo "<a target='_blank' href='$contributor'>$title</a>";
输出应该是这样的:
Item
Item description
Company
Item Title