有没有办法使用 XMLReader 获取第一行?

Is there a way to get the first line using XMLReader?

有没有办法在 PHP 中使用 xmlreader 获取元素的开始标记?

我有这种xml:

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

这是我的代码。 $reader 是 XML 阅读器类型。

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

我希望它得到 <Product id="L20" manufacturer="A"> 作为输出。

我想指定我想使用 XMLReader。当他们使用 DOM 或简单 XML 时,不要暗示它是其他人的副本。我有一个很大的 XML 文件,在我当前的系统上无法将其全部放入内存。

您需要使用 XMLReader 方法读取节点的名称和属性。似乎 XMLReader::readString() 没有为属性节点实现,所以您需要收集名称,导航回元素并使用 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,
                )
            )
        );
    }
}

输出:

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

可以将 XMLReader 与 DOM 结合使用。大 XML 文件通常是项目列表。您使用 XMLReader 查找项目节点并将其扩展为 DOM 以获得更复杂的内容。如果您 XML 是 Product 节点的列表,您可以迭代和扩展它们。它只会一次将 Product 节点及其后代加载到内存中,允许您使用 DOM 方法和 Xpath 表达式。

$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');
}

输出:

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"
}