2010-08-01 27 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();