DOMdocument:使用nodeValue更改html内容会生成错误格式的html文件(< div& gt;而不是< div>) [英] DOMdocument: using nodeValue to change html content produces wrongly formatted html file ( <div> instead of <div>)

查看:68
本文介绍了DOMdocument:使用nodeValue更改html内容会生成错误格式的html文件(< div& gt;而不是< div>)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jquery让用户定位各种div。之后,我通过jQuery的$ .post将它们转储到php POST函数中,并通过php将它们保存到外部html文件中。应该处理写入的.php文件使用DOMdocument(我从来没有用过 - 我也没有任何XML约定知识,如果有的话)。



.php文件的内容如下:

 <?php 
$ data = $ _POST ['data'];
$ doc = new DOMDocument();
$ doc-> loadHTMLFile(startfile.html);
$ doc-> getElementById('container') - > nodeValue = $ data;
$ doc-> saveHTMLFile(endfile.html)
?>

这非常简单,它应该可以工作。 Howewer,而endfile.html输出应该是:

 < div id =container> 
< div class =dummydata>
< / div>
< / div>

实际产出

 < div id =container> 
& lt; / class =dummydata& gt;
& lt; / div& gt;
< / div>

浏览器当然无法正确解析。我相信我错过了一些简单的东西,一旦解决方案出现在我眼前,我会感到非常愚蠢。我不想求助于fopen / fwrite,因为这看起来像是一种处理html文件的优雅灵活的方式。

因为建议的XML片段解析由于格式不正确而保持失败(可能只是考虑因素,因为我基本上是通过工作的HTML代码 - 可能是XML对于使用的间距和字符更明智吗? hacky解决方案:

  //用nodevalue编写HTML $ b $ doc-> getElementById('container') - > ; nodeValue = $ data; 
$ doc-> saveHTMLFile(../ test.html);
//打开文件并替换stripped<> s
$ file_path ='.. / test.html'
$ content = file_get_contents($ file_path);
$ content = str_replace('& lt;','<',$ content);
$ content = str_replace('& gt;','>',$ content);
file_put_contents($ file_path,$ content);

这不是非常优雅,但它可以工作houghts?你认为这样一个拙劣的解决方案可能是合理的,而不是试图正确格式化HTML作为XML传递?什么是可能的陷阱?我想不出任何,看到我只是克隆格式不正确,但工作的HTML。提前致谢。

解决方案

是的, nodeValue 将剥离标签。



您需要创建数据片段,然后

 <?php $ b /http://domdocumentfragment.appendxml.phprel =nofollow>追加
$ b $ data ='< div class =dummydata>< / div>';
$ doc = new DOMDocument();
$ doc-> loadHTML('<!DOCTYPE html>< div id =container> contents< / div>');

$ el = $ doc-> getElementById('container');

$ children = $ el-> childNodes;
while($ children-> length)
$ el-> removeChild($ children-> item(0));

$ frag = $ el-> ownerDocument-> createDocumentFragment();
$ frag-> appendXML($ data);
$ el-> appendChild($ frag);

echo $ doc-> saveHTML();

如果您想要整合它,请务必用您的代码替换loadHTML和saveHTML。



输出

 <!DOCTYPE html> 
< html>< body>< div id =container>< div class =dummydata>< / div>< / div>< / body>< HTML>


I am using jquery to let the user position various divs. Afterwards I'm dumping them to a php POST function via jquery's $.post and save them to an external html file through php. The .php file which should handle the writing uses DOMdocument (which I have never used- I also have no knowledge of any XML conventions, if there are any).

The .php file reads:

<?php
$data = $_POST['data'];
$doc = new DOMDocument();
$doc->loadHTMLFile("startfile.html");
$doc->getElementById('container')->nodeValue = $data;
$doc->saveHTMLFile("endfile.html")
?>

It's really simple, and it should work. Howewer, whereas the endfile.html output should be:

<div id="container">
<div class= "dummydata">
</div>
</div>

it actually outputs

<div id="container">
 &lt;/class= "dummydata"&gt;
 &lt;/div&gt;
</div>

Which of course cannot be correctly parsed by the browser. I am sure I've been missing something simple and I'll feel extremely stupid once the solution is before my eyes. I don't want to resort to fopen/fwrite as this seemed like a very elegant and flexible way of handling html files.

edit: since the suggested XML fragment parsing kept failing due to unproper formatting (probably just cosmeting, seeing as i'm basically passing working HTML code - might it be XML is much more sensible in regard to spacing and characters used?- i went with this very hacky solution:

//write HTML with nodevalue
$doc->getElementById('container')->nodeValue = $data;
$doc->saveHTMLFile("../test.html");
//open file and replace stripped < and >s
$file_path ='../test.html'
$content = file_get_contents($file_path);
$content = str_replace('&lt;', '<', $content);
$content = str_replace('&gt;', '>', $content);
file_put_contents($file_path, $content);

It's not very elegant, but it works. Any thoughts? Do you think such a hacky solution might be justified instead of trying to properly format the HTML i'm passing as XML? And what are some possible pitfalls? I can't think of any, seeing i'm just cloning badly formatted but working HTML. Thanks in advance.

解决方案

Yep, nodeValue will strip the tags.

You'll want to create a fragment of the data, and then append it.

<?php

$data = '<div class="dummydata"></div>';
$doc = new DOMDocument();
$doc->loadHTML('<!DOCTYPE html><div id="container">contents</div>');

$el = $doc->getElementById('container');

$children = $el->childNodes;
while ($children->length)
    $el->removeChild($children->item(0));

$frag = $el->ownerDocument->createDocumentFragment();
$frag->appendXML($data);
$el->appendChild($frag);

echo $doc->saveHTML();

Be sure to replace the loadHTML and saveHTML with your code if you want to integrate it.

Output:

<!DOCTYPE html>
<html><body><div id="container"><div class="dummydata"></div></div></body></html>

这篇关于DOMdocument:使用nodeValue更改html内容会生成错误格式的html文件(&lt; div&amp; gt;而不是&lt; div&gt;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆