本文介绍了如何将HTML文档汇编到DOMDocument中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于表示HTML文档部分的文件,似乎填写 loadHTML loadHTMLFile c $ c> html 和 body 每个部分的标签,当我输出以下内容时显示:

  $ doc = new DOMDocument(); 
$ doc-> loadHTMLFile($ file);
$ elements = $ doc-> getElementsByTagName('*');

if(!is_null($ elements)){
foreach($ elements as $ element){
echo< br />。 $元素 - >节点名称。 :;

$ nodes = $ element-> childNodes;
foreach($ nodes as $ node){
echo $ node-> nodeValue。 \\\
;
}
}
}

由于我打算组装这些部分进入我自己的代码中的较大的文档,我已经被指示使用DOMDocument来做到这一点,可以做些什么来防止这种行为?

解决方案

最接近的是使用。



然后你可以做:

  $ doc = new DOMDocument(); 
...
$ f = $ doc-> createDocumentFragment();
$ f-> appendXML(< foo> text< / foo>< bar> text2< / bar>);
$ someElement-> appendChild($ f);

但是,这需要XML,而不是HTML。



无论如何,我想你正在创造一个人为的问题。因为你知道的行为是创建 html body 标签,你可以从内部提取文件中的元素body标签,然后导入到组装最终文件的DOMDocument中。请参阅。


It appears that loadHTML and loadHTMLFile for a files representing sections of an HTML document seem to fill in html and body tags for each section, as revealed when I output with the following:

$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$elements = $doc->getElementsByTagName('*');

if( !is_null($elements) ) {
    foreach( $elements as $element ) {
        echo "<br/>". $element->nodeName. ": ";

        $nodes = $element->childNodes;
        foreach( $nodes as $node ) {
            echo $node->nodeValue. "\n";
        }
    }
}

Since I plan to assemble these parts into the larger document within my own code, and I've been instructed to use DOMDocument to do it, what can I do to prevent this behavior?

解决方案

The closest you can get is to use the DOMDocumentFragment.

Then you can do:

$doc = new DOMDocument();
...
$f = $doc->createDocumentFragment();
$f->appendXML("<foo>text</foo><bar>text2</bar>"); 
$someElement->appendChild($f);

However, this expects XML, not HTML.

In any case, I think you're creating an artificial problem. Since you know the behavior is to create the html and body tags you can just extract the elements in the file from within the body tag and then import the, to the DOMDocument where you're assembling the final file. See DOMDocument::importNode.

这篇关于如何将HTML文档汇编到DOMDocument中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 05:23