略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: XMLReader::expand

2024-11-22

XMLReader::expand

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

XMLReader::expandReturns a copy of the current node as a DOM object

说明

public XMLReader::expand(?DOMNode $baseNode = null): DOMNode|false

This method copies the current node and returns the appropriate DOM object.

参数

baseNode

A DOMNode defining the target DOMDocument for the created DOM object.

返回值

The resulting DOMNode or false on error.

add a noteadd a note

User Contributed Notes 1 note

up
0
Sbastien
5 months ago
XMLReader::expand() expands the current subtree to DOM. SimpleXML is good too. To avoid a warning like "Imported Node must have associated Document" when using simplexml_import_dom(), we can do :

<?php

// Huge XML compressed file
$xml = XMLReader::open('compress.zlib:///path/to/my-data.xml.gz');

// Targeting a tiny subtree
while ($xml->name !== 'my-targeted-element') {
   
$xml->read();
}

// The trick is here...
// Subtree is expanded in an empty document...
$dom = $xml->expand(new DOMDocument());

// ... which can be imported by SimpleXML
$sx = simplexml_import_dom($dom);

// We can now process our tiny subtree with SimpleXML $sx

官方地址:https://www.php.net/manual/en/xmlreader.expand.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3