douji6940 2017-11-07 14:25
浏览 50
已采纳

使用PHP循环遍历重复的XML实体

I am trying to use PHP to read a large XML file (gzipped). The file consists of repeated products (actually books). Each book has 1 or more contributors. This is an example of a product.

<Product>

    <ProductIdentifier>
        <IDTypeName>EAN.UCC-13</IDTypeName>
        <IDValue>9999999999999</IDValue>
    </ProductIdentifier>
    <Contributor>
        <SequenceNumber>1</SequenceNumber>
        <ContributorRole>A01</ContributorRole>
        <PersonNameInverted>Bloggs, Joe</PersonNameInverted>
    </Contributor>
    <Contributor>
        <SequenceNumber>2</SequenceNumber>
        <ContributorRole>A01</ContributorRole>
        <PersonNameInverted>Jones, John</PersonNameInverted>
    </Contributor>
            <Contributor>
        <SequenceNumber>3</SequenceNumber>
        <ContributorRole>B01</ContributorRole>
        <PersonNameInverted>Other, An</PersonNameInverted>
    </Contributor>

The output I would wish for this example is

Array
(
    [1] => 9999999999999
    [2] => Bloggs, Joe(A01)
    [3] => Jones, John(A01)
    [4] => Other, An(B01)

)

My code loads the gzipped XML file and handles the repeated sequence of products with no problem but I cannot get it to handle the repeated sequence of contributors. My code for handling the products and first contributor is shown below but I have tried various ways of looping through the contributors but cannot seem to achieve what I need. I'm a beginner with PHP and XML although an IT professional for many years.

  $reader = new XMLReader();

//load the selected XML file to the DOM
if(!$reader->open("compress.zlib://filename.xml.gz","r")){
  die('Failed to open file!');
  }

while ($reader->read()):

  if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'Product')
        {
        $xml = simplexml_load_string($reader->readOuterXML());
        list($result) = $xml->xpath('//ProductIdentifier[IDTypeName = "EAN.UCC-13"]');
        $line[1] = (string)$result->IDValue;  
        list($result) = $xml->xpath('//Contributor');
        $contributorname = (string)$result->PersonNameInverted;
        $role = (string)$result->ContributorRole;
        $line[2] = $contributorname."(".$role.")";
        echo '<pre>'; print_r($line); echo '</pre>';
        }

endwhile;
  • 写回答

1条回答 默认 最新

  • dqbn76906 2017-11-07 15:01
    关注

    Since you have several contributors, you must handle it as an array and loop on them to prepare your final variable:

    <?php
    
    $reader = new XMLReader();
    //load the selected XML file to the DOM
    if(!$reader->open("compress.zlib://filename.xml.gz","r")){
      die('Failed to open file!');
    }
    
    while ($reader->read()) {
      if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'Product') {
        $xml = simplexml_load_string($reader->readOuterXML());
        list($result) = $xml->xpath('//ProductIdentifier[IDTypeName = "EAN.UCC-13"]');
        $line[1] = (string)$result->IDValue;
    
        // get all contributors in an array
        $contributors = $xml->xpath('//Contributor');
    
        $i = 2;
        // go through all contributors
        foreach($contributors as $contributor) {
           $contributorname = (string)$contributor->PersonNameInverted;
           $role = (string)$contributor->ContributorRole;
           $line[$i] = $contributorname."(".$role.")";
           $i++;
        }
        echo '<pre>'; print_r($line); echo '</pre>';
    
      }
    }
    

    This will give you the following output:

    Array
    (
      [1] => 9999999999999
      [2] => Bloggs, Joe(A01)
      [3] => Jones, John(A01)
      [4] => Other, An(B01)
    )
    

    EDIT: Some explanation here on what is wrong on your code. Instead of taking all the contributors, you just take the first one with list()

    http://php.net/manual/en/function.list.php (assign all values of an array into variables). Since you don't know how many contributors you have (i guess...), you cannot use this.

    Then you assign the first one into your $line, so you always have only the first one.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题