从XML文件接收特定值

时间:2018-09-21 15:11:25

标签: php xml simplexml

我有以下这段XML:

<?xml version="1.0" encoding="UTF-8"?>
<searchRetrieveResponse xmlns="http://www.loc.gov/zing/srw/">
   <version>1.1</version>
   <numberOfRecords>1</numberOfRecords>
   <records>
      <record>
     <recordSchema>MARC21-xml</recordSchema>
     <recordPacking>xml</recordPacking>
     <recordData>
        <record xmlns="http://www.loc.gov/MARC21/slim" type="Authority">
           <leader>00000nz  a2200000nc 4500</leader>
           <controlfield tag="001">040432386</controlfield>
           <controlfield tag="003">DE-101</controlfield>
           <controlfield tag="005">20180124053847.0</controlfield>
           <controlfield tag="008">880701n||azznnbabn           | ana    |c</controlfield>
           <datafield tag="024" ind1="7" ind2=" ">
          <subfield code="a">http://d-nb.info/gnd/4043238-5</subfield>
          <subfield code="2">uri</subfield>
           </datafield>
           <datafield tag="035" ind1=" " ind2=" ">
          <subfield code="a">(DE-101)040432386</subfield>
           </datafield>
           <datafield tag="035" ind1=" " ind2=" ">
          <subfield code="a">(DE-588)4043238-5</subfield>
           </datafield>
           <datafield tag="035" ind1=" " ind2=" ">
          <subfield code="z">(DE-588c)4043238-5</subfield>
          <subfield code="9">v:zg</subfield>
           </datafield>
           <datafield tag="040" ind1=" " ind2=" ">
          <subfield code="a">DE-101</subfield>
          <subfield code="9">r:DE-101</subfield>
          <subfield code="b">ger</subfield>
          <subfield code="d">9999</subfield>
           </datafield>
           <datafield tag="042" ind1=" " ind2=" ">
          <subfield code="a">gnd1</subfield>
           </datafield>
           <datafield tag="065" ind1=" " ind2=" ">
          <subfield code="a">13.3</subfield>
          <subfield code="2">sswd</subfield>
           </datafield>
           <datafield tag="075" ind1=" " ind2=" ">
          <subfield code="b">s</subfield>
          <subfield code="2">gndgen</subfield>
           </datafield>
           <datafield tag="075" ind1=" " ind2=" ">
          <subfield code="b">saz</subfield>
          <subfield code="2">gndspec</subfield>
           </datafield>
           <datafield tag="079" ind1=" " ind2=" ">
          <subfield code="a">g</subfield>
          <subfield code="q">s</subfield>
          <subfield code="u">w</subfield>
          <subfield code="u">o</subfield>
          <subfield code="u">z</subfield>
           </datafield>
           <datafield tag="083" ind1="0" ind2="4">
          <subfield code="a">751.45</subfield>
          <subfield code="9">d:3</subfield>
          <subfield code="9">t:2007-01-01</subfield>
          <subfield code="2">22/ger</subfield>
           </datafield>
           <datafield tag="150" ind1=" " ind2=" ">
          <subfield code="a">Ölmalerei</subfield>
           </datafield>
           <datafield tag="450" ind1=" " ind2=" ">
          <subfield code="a">Ölgemälde</subfield>
           </datafield>
           <datafield tag="550" ind1=" " ind2=" ">
          <subfield code="0">(DE-101)040372200</subfield>
          <subfield code="0">(DE-588)4037220-0</subfield>
          <subfield code="0">http://d-nb.info/gnd/4037220-0</subfield>
          <subfield code="a">Malerei</subfield>
          <subfield code="4">obal</subfield>
          <subfield code="4">http://d-nb.info/standards/elementset/gnd#broaderTermGeneral</subfield>
          <subfield code="w">r</subfield>
          <subfield code="i">Oberbegriff allgemein</subfield>
           </datafield>
           <datafield tag="670" ind1=" " ind2=" ">
          <subfield code="a">M</subfield>
           </datafield>
           <datafield tag="677" ind1=" " ind2=" ">
          <subfield code="a">Wird i.d.R. nur verwendet, wenn die Maltechnik behandelt ist.</subfield>
           </datafield>
           <datafield tag="750" ind1=" " ind2="7">
          <subfield code="a">Peinture à l'huile</subfield>
          <subfield code="0">(FrPBN)FRBNF119329441</subfield>
          <subfield code="0">http://data.bnf.fr/11932944</subfield>
          <subfield code="2">ram</subfield>
          <subfield code="9">v:MACS-Mapping. Bitte keine Änderungen vornehmen.</subfield>
           </datafield>
           <datafield tag="913" ind1=" " ind2=" ">
          <subfield code="S">swd</subfield>
          <subfield code="i">s</subfield>
          <subfield code="a">Ölmalerei</subfield>
          <subfield code="0">(DE-588c)4043238-5</subfield>
           </datafield>
        </record>
     </recordData>
     <recordPosition>1</recordPosition>
      </record>
   </records>
   <nextRecordPosition>2</nextRecordPosition>
   <echoedSearchRetrieveRequest>
      <version>1.1</version>
      <query>WOE=4043238-5 and COD=s</query>
      <xQuery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
      <recordSchema>MARC21-xml</recordSchema>
   </echoedSearchRetrieveRequest>
   <extraResponseData>
      <accountOf xmlns="">Staatsbibliothek zu Berlin - Preußischer Kulturbesitz, bpk-Bildagentur</accountOf>
   </extraResponseData>
</searchRetrieveResponse>

当我在下面运行我的PHP代码时,我只会得到一个空白页。例如,缺少datafield 150和相应的tag来访问subfield的哪个代码?我想念什么?

php代码:

header('Content-type: text/html; charset=utf-8');
error_reporting(E_ALL);
$id = $_POST["id"];             

// URL to fetch
$url = "https://services.dnb.de/sru/authorities?version=1.1&operation=searchRetrieve&query=WOE%3D$id%20and%20COD%3Ds&recordSchema=MARC21-xml";

// Setting the HTTP Request Headers
$User_Agent = 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0';

$request_headers = array('Contect-Type:text/xml', 'Accept:text/xml');

$ch = curl_init($url);
// Set the url
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute
$result = curl_exec($ch); // Performs the Request, with specified curl_setopt() options (if any).

// Closing
curl_close($ch);
$xml = SimpleXMLElement($result) or die("Error: Cannot create object");
echo($xml);

我环顾四周,发现可能与将其传递给字符串有关,但随后在屏幕上却什么也没得到。甚至没有SimpleXMLElement对象。

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

SimpleXMLElement是一个类名,而不是一个函数。尝试使用new实例化它:

$xml = new SimpleXMLElement($result);

或者改用simplexml_load_string。

$xml = simplexml_load_string($result);

此外,使用所有curl代码会使事情变得过于复杂。这应该同样有效:

// ALWAYS sanitize user-submitted data
// in this example everything except numbers and hyphens are removed
$id = preg_replace("/[^0-9-]/", "", $_POST["id"]);
$url = "https://services.dnb.de/sru/authorities?version=1.1&operation=searchRetrieve&query=WOE%3D$id%20and%20COD%3Ds&recordSchema=MARC21-xml";
$xml = new SimpleXMLElement($url, 0, true);

答案 1 :(得分:0)

评论和其他答案涵盖了您未获得任何输出的原因。这是因为缺少new关键字。这将向您展示如何获取结果对象并解析它以获得所需的子字段。这只是转储子字段对象,但是您可以从此处使用它进行任何操作。

$xmlObj = new SimpleXMLElement($xml);

foreach($xmlObj->records->record->recordData->record->datafield as $dfield)
{
    if((string)$dfield["tag"] == "150")
    {
        $subfield = $dfield->subfield;
        var_dump($subfield);
    }
}

DEMO