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

Php 有没有办法使用XMLReader获取第一行?,php,xml,xml-parsing,xmlreader,Php,Xml,Xml Parsing,Xmlreader,有没有办法在PHP中使用xmlreader获取元素的开始标记 我有这种类型的xml: <Product id="L20" manufacturer="A"> <Description>Desc</Description> <Price>5.00</Price> </Product> 我希望它得到作为输出 我想指定我想使用XMLReader。当其他人使用DOM或simpleXML时,不要再暗示它是他们的复制品

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

我有这种类型的xml:

<Product id="L20" manufacturer="A">
    <Description>Desc</Description>
    <Price>5.00</Price>
</Product>
我希望它得到
作为输出


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

您需要使用XMLReader方法读取节点的名称和属性。似乎属性节点没有实现
XMLReader::readString()
,因此您需要收集名称,导航回元素并使用
XMLReader::getAttribute()

可以将XMLReader与DOM结合起来。大型XML文件通常是项目列表。您可以使用XMLReader查找item节点,并将其扩展到DOM中以获得更复杂的内容。如果XML是
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');
}

您需要使用XMLReader方法读取节点的名称和属性。似乎属性节点没有实现
XMLReader::readString()
,因此您需要收集名称,导航回元素并使用
XMLReader::getAttribute()

可以将XMLReader与DOM结合起来。大型XML文件通常是项目列表。您可以使用XMLReader查找item节点,并将其扩展到DOM中以获得更复杂的内容。如果XML是
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');
}

使用
XMLReader
:您是否知道它是一个
产品
标记,或者无论它是什么类型,您是否需要能够获取第一个标记?我希望获取第一个标记,无论它是什么类型。我应该指定,我不希望使用DOMDocument或SimpleXML作为可能的重复项。我的XML大约是3GB大小-这就是为什么我问XMLReader中是否有方法。使用
XMLReader
:您知道它是
产品
标记吗,或者您是否需要能够获得第一个标记,无论它是什么类型?我希望获得第一个标记,无论它是什么类型。我应该指定,我不想像建议的那样使用DOMDocument或SimpleXML作为可能的副本。我的XML大约是3GB大小——这就是为什么我问XMLReader中是否有一种方法。
string(7) "Product"
array(2) {
  ["id"]=>
  string(3) "L20"
  ["manufacturer"]=>
  string(1) "A"
}
$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"
}