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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?