2017-10-14 90 views
0

從XML URL值我有這個網址:如何直接訪問使用PHP

http://lx2.loc.gov:210/lcdb?operation=searchRetrieve&recordSchema=marcxml&version=1.1&maximumRecords=10&query=bath.isbn%3D9781452110103

它返回一個XML響應,我要取它的價值的情況下直接通過整個內容循環,到目前爲止,我僅訪問了$str=$xml->getName();的XML根節點的名稱。那麼有沒有什麼辦法可以訪問其餘的節點。

以下是從URL獲取返回結果的示例XML。

<zs:searchRetrieveResponse xmlns:zs="http://www.loc.gov/zing/srw/"> 
<zs:version>1.1</zs:version> 
<zs:numberOfRecords>2</zs:numberOfRecords> 
<zs:records> 
<zs:record> 
<zs:recordSchema>marcxml</zs:recordSchema> 
<zs:recordPacking>xml</zs:recordPacking> 
<zs:recordData> 
<record xmlns="http://www.loc.gov/MARC21/slim"> 
<leader>01517cam a2200277 i 4500</leader> 
<controlfield tag="001">17092185</controlfield> 
<controlfield tag="005">20150720141836.0</controlfield> 
<controlfield tag="008">111221s2011 caua 000 0 eng</controlfield> 
<datafield tag="906" ind1=" " ind2=" "> 
<subfield code="a">7</subfield> 
<subfield code="b">cbc</subfield> 
<subfield code="c">orignew</subfield> 
<subfield code="d">1</subfield> 
<subfield code="e">ecip</subfield> 
<subfield code="f">20</subfield> 
<subfield code="g">y-gencatlg</subfield> 
</datafield> 
</zs:recordData> 
<zs:recordPosition>2</zs:recordPosition> 
</zs:record> 
</zs:records> 
<zs:echoedSearchRetrieveRequest> 
<zs:version>1.1</zs:version> 
<zs:query>bath.isbn=9781452110103</zs:query> 
<zs:maximumRecords>10</zs:maximumRecords> 
<zs:recordPacking>xml</zs:recordPacking> 
<zs:recordSchema>marcxml</zs:recordSchema> 
</zs:echoedSearchRetrieveRequest> 
</zs:searchRetrieveResponse> 
+0

使用PHP的XML解析器: - https://www.w3schools.com/php/php_xml_simplexml_read。 asp –

+0

我已經用'simplexml_load_file'完成了。 –

+0

您能否提供預期的產出? – mega6382

回答

0

也許這是一個選項,供您使用simplexml_load_filexpath

首先你必須註冊namespace。 然後,您可以更改正在搜索的東西的xpath表達式。

例如:

$xml = simplexml_load_file("http://lx2.loc.gov:210/lcdb?operation=searchRetrieve&recordSchema=marcxml&version=1.1&maximumRecords=10&query=bath.isbn%3D9781452110103"); 
$xml->registerXPathNamespace("slim", "http://www.loc.gov/MARC21/slim"); 
$path = '/zs:searchRetrieveResponse/zs:records/zs:record[1]/zs:recordData/slim:record[1]/slim:datafield[1]/slim:subfield[@code="c"]'; 
$result = $xml->xpath($path)[0]; 
echo $result; 

會導致:

orignew

+0

非常感謝。它像一個魅力。 –