Is this the best way to import the body of an (HTML) DOMDocument into another? [duplicate]
Possible Duplicate:
How to insert HTML to PHP DOMNode?
Continuing off of this question, here's the code I'm about to start using:
function getHTMLDOMSegment( $file )
{
$doc = new DOMDocument();
$doc->loadHTMLFile( $file );
$body = $dom->getElementsByTagName('body')->item(0);
return $body->childNodes;
}
Then I'd simply iterate over the children, using importNode and append each wherever they need to go in another HTML-loaded DOMDocument.
Does this sound right?
Edit: To be clear, since the source files I'm working with may not be "proper" XHTML, I need to go through loadHTMLFile like this no matter what, apparently.
Also, because this might be having to work with a large amount of HTML content, my goal is to be efficient as well.
I am somewhat reluctant to answer this question because it is basically just a longer version of what Artefacto and I already told you, but anyway.
You can either add the raw XML subtree and add it through a fragment
DOMDocumentFragment::appendXML
— Append raw XML data
Or use
DOMDocument::importNode
— Import node into current document
Note that you can deep import entire trees by passing TRUE
as the second argument.
Examples:
$a = <<< HTML
<div>
<h2>Hello World</h2>
<p> I am from the <abbr title="source">src</abbr> document<br/></p>
</div>
HTML;
$b = <<< HTML
<h1>Importing Fragments</h1>
HTML;
Using fragments:
$dest = new DOMDocument;
$dest->loadHTML($b);
$fragment = $dest->createDocumentFragment();
$fragment->appendXML($a);
$dest->getElementsByTagName('h1')->item(0)->appendChild($fragment);
echo $dest->saveHTML();
Using imports:
$dest = new DOMDocument;
$dest->loadHTML($b);
$src = new DOMDocument;
$src->loadHTML($a);
$dest->getElementsByTagName('h1')->item(0)->appendChild(
$dest->importNode(
$src->getElementsByTagName('div')->item(0),
TRUE
)
);
echo $dest->saveHTML();
The output for both of these would be something like
<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><h1>Importing Fragments<div>
<h2>Hello World</h2>
<p> I am from the <abbr title="source">src</abbr> document<br></p>
</div></h1></body></html>
If you are concerned about which one performs better, I suggest to benchmark these under real world conditions, e.g. in your application. Then judge for yourself which suits your needs.
精彩评论