2010-08-01 25 views
1

我试图围绕PHP和XML包装我的头。通过PHP检索XML页面的元素

我试图做一些事情:

还有就是我通过卷曲检索XML文档(也尝试过各种PHP的XML库参数,如XMLReader::open($url)等回收的方法并不重要;我能够并且已经得到了这部分工作

问题是解析检索页面上的XML

这里是XML的一个例子:。

http://z3950.loc.gov:7090/voyager?version=1.1&operation=searchRetrieve&query=9780471615156&maximumRecords=1&recordPacking=xml&recordSchema=marcxml

我需要从该页面获得的是电话号码;

<datafield tag="060" ind1=" " ind2=" "> 
    <subfield code="a">WM 173.6 R823m</subfield> 
</datafield> 

作者;

<datafield tag="100" ind1="1" ind2=" "> 
    <subfield code="a">Ross, Colin A.</subfield> 
</datafield> 

和标题信息;

<datafield tag="245" ind1="1" ind2="0"> 
    <subfield code="a">Multiple personality disorder :</subfield> 
    <subfield code="b">diagnosis, clinical features, and treatment /</subfield> 
    <subfield code="c">Colin A. Ross.</subfield> 
</datafield> 

看起来够简单。然而,对于我来说,我似乎无法获得任何内置的PHP函数来处理XML的工作(因为我做错了)。

这里是我试过的例子:

//xml file retrieved via curl and saved to folder 
$file="9780471615156.xml"; 

$xml = simplexml_load_file($file); 

echo $xml->getName();//returns searchRetrieveResponse 

foreach($xml->searchRetrieveResponse[0]->attributes() as $a => $b){ 
    echo $a,'="',$b,"\"</br>";//nothing 
} 

foreach ($xml->searchRetrieveResponse[0]->children() as $child){ 
    echo "Child node: " . $child . "<br />";//nothing 
} 

它返回第一个节点的名称,但我不能让它去任何更深。

注:我运行PHP 5+

回答

1

据我试过的SimpleXML无法读取该XML。试试下面的例子,它会列出一个数组,你可以很容易地循环和找到你需要的东西,只需比较你正在寻找的键/值。

// load XML into string here 
// $string = ????; 
$xml_parser = xml_parser_create(); 
xml_parse_into_struct($xml_parser, $string, $object, $index); 

echo '<pre>'; 
print_r($object); 
// print_r($index); 
echo '</pre>'; 
+0

正是我所需要的。非常感谢! – stormdrain 2010-08-01 21:16:13

+0

@stormdrain:我的荣幸:)享受 – dwich 2010-08-01 21:21:38

2

xml_parse_into_struct()可能没有问题。但既然已经指出,这不能用SimpleXML来完成:

<?php 
$file="http://z3950.loc.gov:7090/voyager?version=1.1&operation=searchRetrieve&query=9780471615156&maximumRecords=1&recordPacking=xml&recordSchema=marcxml"; 
$xml = simplexml_load_file($file); 
$xml->registerXPathNamespace('foo', 'http://www.loc.gov/MARC21/slim'); 

foreach($xml->xpath('//foo:record') as $record) { 
    echo "record: \n"; 
    $record->registerXPathNamespace('foo', 'http://www.loc.gov/MARC21/slim'); 
    foreach($record->xpath('foo:datafield[@tag="060" or @tag="100" or @tag="245"]') as $datafield) { 
    switch($datafield['tag']) { 
     case '060': 
     echo " call number: \n"; 
     break; 
     case '100': 
     echo "author: \n"; 
     break; 
     case '245': 
     echo "title : \n"; 
     break; 
    } 
    $datafield->registerXPathNamespace('foo', 'http://www.loc.gov/MARC21/slim'); 
    foreach($datafield->xpath('foo:subfield') as $sf) { 
     echo ' ', $sf['code'] . ': ' . $sf . "\n"; 
    }  
    } 
} 

打印

record: 
    call number: 
    a: WM 173.6 R823m 
author: 
    a: Ross, Colin A. 
title : 
    a: Multiple personality disorder : 
    b: diagnosis, clinical features, and treatment/
    c: Colin A. Ross. 

这是一个有点讨厌,你必须为每个后续的SimpleXMLElement一次又一次注册了命名空间。 ..但无论如何,它的工作原理,它使用了SimpleXML ;-)

还看到:http://docs.php.net/simplexmlelement.registerXPathNamespacehttp://www.w3.org/TR/xpath/

3

由于它出现你正在寻找解析MARCXML,我会建议使用File_MARC PEAR package。要生成像你想要做的代码将看起来大致如下:

<?php 

require_once('File/MARCXML.php'); 
$file="9780471615156.xml"; 
$record = new File_MARCXML($file); 
echo " call number: \n"; 
echo " " . $record->getField('060')['a']; 
echo " author: \n"; 
echo " " . $record->getField('100')['a']; 
echo " title: \n"; 
echo " " . $record->getField('245')->formatField();