douji6940
2017-11-07 14:25
浏览 47

使用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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题