我有这个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>';
}
答案 0 :(得分:1)
是的,你这样做是错误的。如果存在命名空间,则必须使用->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