dou426098 2016-06-23 19:30
浏览 72
已采纳

xpath / php - 使用php选择包含特定属性的XML元素

I'm trying to read data from nodes that have the same attribute names. I would like to read the lines with tag="650" into 3 separate variables. ie: each time I'm in a catalog node I need to read the given subjects.

<report>
<catalog>
    <flexibleKey>123</flexibleKey>
    <numberOfTitleHolds>0</numberOfTitleHolds>
    <totalHolds>0</totalHolds>
    <numberOfCallNumbers>1</numberOfCallNumbers>
    <bibliographicLevel>FULL</bibliographicLevel>
    <catalogFormat>MARC</catalogFormat>
    <createdBy>ADMIN</createdBy>
    <dateCreated>2002-11-20</dateCreated>
    <dateCataloged>2003-02-05</dateCataloged>
    <modifiedBy>ADMIN</modifiedBy>
    <dateModified>2014-08-15</dateModified>
    <marc>
        <marcEntry tag="506" label="Access restriction" ind="  ">Classroom and In Library Use</marcEntry>
        <marcEntry tag="245" label="Title" ind="  ">title</marcEntry>
        <marcEntry tag="500" label="General Note" ind="  ">ATEC</marcEntry>
        <marcEntry tag="520" label="Abstract" ind="  ">info</marcEntry>
        <marcEntry tag="650" label="Subject term" ind=" 0">subject 1</marcEntry>
        <marcEntry tag="650" label="Subject term" ind=" 0">subject 2</marcEntry>
        <marcEntry tag="650" label="Subject term" ind=" 0">subject 3</marcEntry>
    </marc>
</catalog>
<catalog>
    <flexibleKey>456</flexibleKey>
    <numberOfTitleHolds>0</numberOfTitleHolds>
    <totalHolds>0</totalHolds>
    <numberOfCallNumbers>1</numberOfCallNumbers>
    <bibliographicLevel>FULL</bibliographicLevel>
    <catalogFormat>MARC</catalogFormat>
    <createdBy>ADMIN</createdBy>
    <dateCreated>2002-11-20</dateCreated>
    <dateCataloged>2003-02-05</dateCataloged>
    <modifiedBy>ADMIN</modifiedBy>
    <dateModified>2014-08-15</dateModified>
    <marc>
        <marcEntry tag="506" label="Access restriction" ind="  ">Classroom and In Library Use</marcEntry>
        <marcEntry tag="245" label="Title" ind="  ">title</marcEntry>
        <marcEntry tag="500" label="General Note" ind="  ">ATEC</marcEntry>
        <marcEntry tag="520" label="Abstract" ind="  ">info</marcEntry>
        <marcEntry tag="650" label="Subject term" ind=" 0">subject A</marcEntry>
        <marcEntry tag="650" label="Subject term" ind=" 0">subject B</marcEntry>
    </marc>
</catalog>
</report>

My current code is below...

$z = new XMLReader;
$z->open('my.xml');

while ($z->read() && $z->name !== 'catalog');

while ($z->name === 'catalog') {
   $node = simplexml_import_dom($doc->importNode($z->expand(), true));  
   echo $node->flexibleKey;
   echo $node->dateCreated;

   foreach ($node->marc->marcEntry as $tag) {
      // now I get lost :(
   }
}

Thanks for any help you can offer :)

  • 写回答

3条回答 默认 最新

  • dtg25862 2016-06-30 16:25
    关注

    This did the trick...

    $z = new XMLReader;
    $z->open('my.xml');
    
    while ($z->read() && $z->name !== 'catalog');
    
        while ($z->name === 'catalog') {
            $node = simplexml_import_dom($doc->importNode($z->expand(), true));  
            echo $node->flexibleKey."<br />";
            echo $node->dateCreated."<br />";
    
            $result = $node->marc->xpath('marcEntry[@tag="650"]');
            foreach ($result as $subjecttag) {
                echo $subjecttag[0]."<br />";
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 Qcustomplot绘制实时动态曲线
  • ¥20 运用matlab画x-y图
  • ¥15 用idea运行项目,运行tomcat报错:断言失败
  • ¥15 Sqlserver查询链接服务器数据问题
  • ¥15 Bibtex4Word 引用中文文献
  • ¥20 用opencv c/c++ 转换成灰度图,然后做一下直方图均衡,输出mp4文件
  • ¥20 matlab中的双层数值积分
  • ¥50 服务器打印水晶报表问题
  • ¥30 gradle环境下javafx项目如何使用druid连接池
  • ¥15 服务器打印水晶报表问题