XMLReader::expand
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
XMLReader::expand — 現在のノードのコピーを DOM オブジェクトとして返す
説明
このメソッドは、現在のノードをコピーして適切な DOM オブジェクトを返します。
+add a note
User Contributed Notes 1 note
Sbastien ¶
2 years 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
↑ and ↓ to navigate •
Enter to select •
Esc to close
Press Enter without
selection to search using Google