代码之家  ›  专栏  ›  技术社区  ›  stormdrain

通过PHP检索XML页面的元素

  •  1
  • stormdrain  · 技术社区  · 16 年前

    我试着用PHP和XML来概括我的想法。

    我想做点什么:

    我正在通过cURL检索一个XML文档(还尝试了各种PHP XML库参数,例如 XMLReader::open($url)

    问题是解析检索到的页面上的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>
    

    看起来很简单。然而,就我的一生而言,似乎无法让任何用于处理XML的内置PHP函数正常工作(因为我做得不对)。

    //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
    }
    

    它返回第一个节点的名称,但我无法让它更深入。

    3 回复  |  直到 9 年前
        1
  •  1
  •   dwich    16 年前

    // 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>';
    
        2
  •  3
  •   anarchivist    14 年前

    考虑到您似乎希望解析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();
    
        3
  •  2
  •   VolkerK    16 年前

    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.
    

    另请参见: http://docs.php.net/simplexmlelement.registerXPathNamespace http://www.w3.org/TR/xpath/

    推荐文章