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 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。