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.

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

报告相同问题?

悬赏问题

  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿