这是将(HTML)DOMDocument的正文导入另一个文件的最佳方式吗? [英] Is this the best way to import the body of an (HTML) DOMDocument into another?
问题描述
可能重复:
如何将HTML插入PHP DOMNode?a>
继续关闭此问题,以下是我即将开始使用的代码:
function getHTMLDOMSegment($ file)
{
$ doc = new DOMDocument();
$ doc-> loadHTMLFile($ file);
$ body = $ dom-> getElementsByTagName('body') - > item(0);
return $ body-> childNodes;
}
然后我只需遍历孩子,使用 importNode ,并附加每个他们需要进入另一个HTML加载的DOMDocument的任何地方。
这是否正确?
编辑:要清楚,因为源文件我在工作中可能不是正确的XHTML,我需要通过loadHTMLFile这样的,无论什么,显然。
另外,因为这可能是必须工作有大量的HTML内容,我的目标也是有效的。
我有点不愿意回答这个问题因为它基本上只是Artefacto和我已经告诉过的更长版本,但无论如何,但是无论如何,您可以添加原始的XML子树,并通过片段添加它
或使用
请注意,您可以通过传递 TRUE
作为第二个参数来深入导入整个树。
示例:
$ a =<< < HTML
< div>
< h2> Hello World< / h2>
< p>我来自< abbr title =source> src< / abbr>文件<峰; br />< / p为H.
< / div>
HTML;
$ b =<<< HTML
< h1>导入片段< / h1>
HTML;
使用片段:
$ dest = new DOMDocument;
$ dest-> loadHTML($ b);
$ fragment = $ dest-> createDocumentFragment();
$ fragment-> appendXML($ a);
$ dest-> getElementsByTagName('h1') - > item(0) - > appendChild($ fragment);
echo $ dest-> saveHTML();
使用导入:
$ 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();
这两个项目的输出将类似于
<!DOCTYPE html PUBLIC
- // W3C // DTD HTML 4.0 Transitional // EN
http://www.w3 .ORG / TR / REC-HTML40 / loose.dtd>
< html>< body>< h1>导入片段< div>
< h2> Hello World< / h2>
< p>我来自< abbr title =source> src< / abbr>文件<峰; br>< / p为H.
< / div>< / h1>< / body>< / html>
如果您担心哪一个表现更好,我建议在现实世界的条件下进行基准测试,例如在你的应用程序。然后判断自己适合您的需要。
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
Or use
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.
这篇关于这是将(HTML)DOMDocument的正文导入另一个文件的最佳方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!