PHP解析XML中的两级名称空间
我一直在尝试解析以下XML文档中的标题和描述(这只是顶部)。我已经找到了很多关于处理名称空间的信息,但我不知道如何处理这样一种情况:获取使用名称空间的内容,而名称空间是其他名称空间的子对象。任何帮助都将是惊人的PHP解析XML中的两级名称空间,php,xml,xml-parsing,xml-namespaces,php-parser,Php,Xml,Xml Parsing,Xml Namespaces,Php Parser,我一直在尝试解析以下XML文档中的标题和描述(这只是顶部)。我已经找到了很多关于处理名称空间的信息,但我不知道如何处理这样一种情况:获取使用名称空间的内容,而名称空间是其他名称空间的子对象。任何帮助都将是惊人的 <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.op
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
<responseDate>2013-10-04T23:19:36Z</responseDate>
<request verb="ListRecords">https://digital.grinnell.edu/drupal/oai2</request>
<ListRecords>
<record>
<header>
<identifier>oai:digital.grinnell.edu:grinnell_166</identifier>
<datestamp>2013-07-02T20:59:30Z</datestamp>
</header>
<metadata>
<oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
<dc:title xmlns:dc="http://purl.org/dc/elements/1.1/">
Grinnell College Young Innovator for Social Justice Prize Award Ceremony, 2011
</dc:title>
<dc:title xmlns:dc="http://purl.org/dc/elements/1.1/">
Young Innovator for Social Justice Prize Award Ceremony, 2011
</dc:title>
<dc:type xmlns:dc="http://purl.org/dc/elements/1.1/">Video</dc:type>
<dc:description xmlns:dc="http://purl.org/dc/elements/1.1/">
An edited recording of the award ceremony for the first Grinnell College Young Innovator for Social Justice Prize.
</dc:description>
2013-10-04T23:19:36Z
https://digital.grinnell.edu/drupal/oai2
oai:digital.grinnell.edu:grinnell_166
2013-07-02T20:59:30Z
格林内尔学院社会公正青年创新者奖颁奖典礼,2011年
2011年社会公正青年创新者奖颁奖典礼
视频
第一届格林内尔学院社会公正奖青年创新者颁奖典礼的编辑记录。
我一直在尝试使用的代码:
本质上,我一直在尝试使用嵌套的foreach循环获取['oai_dc']的子对象的子对象
<?php
$feed = file_get_contents("https://digital.grinnell.edu/drupal/oai2?verb=ListRecords&metadataPrefix=oai_dc&from=2013-06-28&set=islandora_genericCollection");
$xml = new SimpleXmlElement($feed);
foreach ($xml->ListRecords->record as $entry){
echo $entry->header->identifier;
echo "<br>";
echo $entry->header->datestamp;
echo "<br>";
//Use that namespace
$dc = $entry->children($namespaces['oai_dc']);
foreach ($dc as $dcElement){
$childern = $dcElement->children($namespaces['dc'])->title;
echo $children;
echo "inner loop";
echo $children->title;
echo "<br>";
echo $dcElement->description;
}
}
下面是示例输出(同样,上面列出的XML只是部分XML,而完整的XML太长):
oai:digital.grinnell.edu:grinnell_166
2013-07-02T20:59:30Z
内环
内环
oai:digital.grinnell.edu:grinnell_3268
2013-07-02T23:16:17Z
内环
内环
oai:digital.grinnell.edu:grinnell_209
2013-07-01T16:36:55Z
内环
内环
oai:digital.grinnell.edu:grinnell_3269
2013-07-05T23:16:18Z
内环
内环
oai:digital.grinnell.edu:grinnell_3246
2013-07-17T19:21:41Z
内环
内环
oai:digital.grinnell.edu:grinnell_3326
2013-07-18T13:22:46Z
内环
内环
oai:digital.grinnell.edu:grinnell_3249
2013-07-18T13:30:02Z
内环
内环
oai:digital.grinnell.edu:grinnell_3324
2013-07-18T13:31:01Z
内环
内环
oai:digital.grinnell.edu:Grinnell3327
2013-07-18T23:16:01Z
内环
内环
oai:digital.grinnell.edu:grinnell_3328
2013-07-18T23:16:02Z
内环
内环
oai:digital.grinnell.edu:grinnell_3329
2013-07-18T23:16:02Z
内环
内环
oai:digital.grinnell.edu:grinnell_3300
2013-08-07T19:28:45Z
内环
内环
oai:digital.grinnell.edu:grinnell_3256
2013-08-07T19:32:07Z
内环
内环
oai:digital.grinnell.edu:grinnell_3276
2013-08-07T19:39:05Z
内环
内环
oai:digital.grinnell.edu:grinnell_3279
2013-08-07T19:42:51Z
内环
内环
oai:digital.grinnell.edu:grinnell_3281
2013-08-07T19:44:55Z
内环
内环
oai:digital.grinnell.edu:grinnell_3293
2013-08-07T19:47:21Z
内环
内环
oai:digital.grinnell.edu:grinnell_3277
2013-08-07T19:50:29Z
内环
内环
oai:digital.grinnell.edu:grinnell_3257
2013-08-07T19:52:32Z
内环
内环
oai:digital.grinnell.edu:grinnell_3289
2013-08-07T19:54:31Z
内环
内环
提前谢谢 注册您自己的名称空间前缀。不要依赖文档中使用的名称空间前缀(和自动注册)
$dom=newdomdocument();
$dom->load($dom)https://digital.grinnell.edu/drupal/oai2?verb=ListRecords&metadataPrefix=oai_dc&from=2013-06-28&set=islandora_genericCollection');
$xpath=newdomxpath($dom);
$xpath->registerNamespace('oai','http://www.openarchives.org/OAI/2.0/');
$xpath->registerNamespace('oai-dc','http://www.openarchives.org/OAI/2.0/oai_dc/');
$xpath->registerNamespace('dc','http://purl.org/dc/elements/1.1/');
$records=$xpath->evaluate('/oai:oai-PMH/oai:ListRecords/oai:record',NULL,FALSE);
foreach($记录为$条目){
echo$xpath->evaluate('string(oai:header/oai:identifier)',$entry,FALSE);
回声“
”;
echo$xpath->evaluate('string(oai:header/oai:datestamp)',$entry,FALSE);
回声“
”;
$dc=$xpath->evaluate('oai:metadata/oai-dc:dc/dc:',$entry,FALSE);
foreach($dc作为$dcElement){
开关($dcElement->localName){
“标题”一案:
echo“Title:”.$dcElement->nodeValue;
回声“
”;
打破
案例“描述”:
echo“Description:”.$dcElement->nodeValue;
回声“
”;
打破
}
}
}
$dom = new DOMDocument();
$dom->load('https://digital.grinnell.edu/drupal/oai2?verb=ListRecords&metadataPrefix=oai_dc&from=2013-06-28&set=islandora_genericCollection');
$xpath = new DOMXpath($dom);
$xpath->registerNamespace('oai', 'http://www.openarchives.org/OAI/2.0/');
$xpath->registerNamespace('oai-dc', 'http://www.openarchives.org/OAI/2.0/oai_dc/');
$xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
$records = $xpath->evaluate('/oai:OAI-PMH/oai:ListRecords/oai:record', NULL, FALSE);
foreach ($records as $entry) {
echo $xpath->evaluate('string(oai:header/oai:identifier)', $entry, FALSE);
echo "<br>";
echo $xpath->evaluate('string(oai:header/oai:datestamp)', $entry, FALSE);
echo "<br>";
$dc = $xpath->evaluate('oai:metadata/oai-dc:dc/dc:*', $entry, FALSE);
foreach ($dc as $dcElement){
switch ($dcElement->localName) {
case 'title' :
echo 'Title: '.$dcElement->nodeValue;
echo "<br>";
break;
case 'description' :
echo 'Description: '.$dcElement->nodeValue;
echo "<br>";
break;
}
}
}