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.

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

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法