doudou890510 2019-08-08 22:42
浏览 149
已采纳

有没有办法使用XMLReader获取第一行? [重复]

This question is an exact duplicate of:

Is there a way to get the opening tag of the element using xmlreader in PHP?

I have this type of xml:

<Product id="L20" manufacturer="A">
    <Description>Desc</Description>
    <Price>5.00</Price>
</Product>

This is my code. $reader is an XMLReader type.

while($reader->read()) {
    if($reader->nodeType == XMLReader::ELEMENT) {
        //??
    }
}

I want it to get <Product id="L20" manufacturer="A"> as the output.

</div>
  • 写回答

1条回答 默认 最新

  • douhuigang9550 2019-08-09 21:33
    关注

    You would need to read the name and the attributes of the node using XMLReader methods. It seems that XMLReader::readString() is not implemented for attribute nodes, so you would need to collect the names, navigate back to the element and use XMLReader::getAttribute():

    $xml = <<<'XML'
    <Product id="L20" manufacturer="A">
        <Description>Desc</Description>
        <Price>5.00</Price>
    </Product>
    XML;
    
    $reader = new XMLReader();
    $reader->open('data://text/plain;base64,' . base64_encode($xml));
    
    while ($reader->read()) {
        if ($reader->nodeType === XMLReader::ELEMENT && $reader->localName === 'Product') {
            var_dump($reader->localName);
            $attributeNames = [];
            $found = $reader->moveToFirstAttribute();
            while ($found && $reader->nodeType === XMLReader::ATTRIBUTE) {
                $attributeNames[] = $reader->localName;
                $found = $reader->moveToNextAttribute();
            }
            $reader->moveToElement();
            var_dump(
                array_combine(
                    $attributeNames,
                    array_map(
                        function($name) use ($reader) {
                            return $reader->getAttribute($name);
                        },
                        $attributeNames,
                    )
                )
            );
        }
    }
    

    Output:

    string(7) "Product"
    array(2) {
      ["id"]=>
      string(3) "L20"
      ["manufacturer"]=>
      string(1) "A"
    }
    

    It is possible to combine XMLReader with DOM. Large XML files are lists of items usually. You look for the item node using XMLReader and expand it into DOM for the more complex stuff. If you XML is a list of Product nodes you can iterate and expand them. It will only load the Product node and its descendants into memory at once, allow you to use DOM methods and Xpath expressions.

    $xml = <<<'XML'
    <Products>
    <Product id="L20" manufacturer="A">
        <Description>Desc</Description>
        <Price>5.00</Price>
    </Product>
    <Product id="L30" manufacturer="B">
        <Description>Desc</Description>
        <Price>5.00</Price>
    </Product>
    </Products>
    XML;
    
    $reader = new XMLReader();
    $reader->open('data://text/plain;base64,' . base64_encode($xml));
    
    // a document to expand to
    $document = new DOMDocument();
    
    while ($reader->read() && $reader->localName !== 'Product') {
    
    }
    
    while ($reader->nodeType === XMLReader::ELEMENT && $reader->localName === 'Product') {
        $productNode = $reader->expand($document);
        var_dump($productNode->localName);
        var_dump(
            array_map(
                function($node) {
                    return $node->textContent;
                },
                iterator_to_array($productNode->attributes)
            )
        );
    
        // next Product sibling
        $reader->next('Product');
    }
    

    Output:

    string(7) "Product"
    array(2) {
      ["id"]=>
      string(3) "L20"
      ["manufacturer"]=>
      string(1) "A"
    }
    string(7) "Product"
    array(2) {
      ["id"]=>
      string(3) "L30"
      ["manufacturer"]=>
      string(1) "B"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b