php domdocument异常loadHTMLFile
我的脚本中有奇怪行为。这让我对
脚本1感到困惑。
$dom = new DOMDocument();
$dom->loadHTMLFile("html/signinform.html");//loads file here
$form = $dom->getElementsByTagName("form")->item(0);
$div = $dom->createElement("div");
$dom->appendChild($div)->appendChild($form);
echo $dom->saveHTML();
脚本2。
$dom = new DOMDocument();
$div = $dom->createElement("div");
$dom->loadHTMLFile("html/signinform.html");//loads file here
$form = $dom->getElementsByTagName("form")->item(0);
$dom->appendChild($div)->appendChild($form);
echo $dom->saveHTML();
脚本1工作没有问题。它显示了形式。但是脚本2抛出以下错误:致命错误:未捕获异常“DOMException”,消息“错误文档错误”在C:\ Users中
有人可以向我解释为什么仅仅如此吗?更改 loadHTMLFile 函数的位置会导致此类错误?谢谢
I am having a strange behavior in my script. That has me confused
Script 1.
$dom = new DOMDocument();
$dom->loadHTMLFile("html/signinform.html");//loads file here
$form = $dom->getElementsByTagName("form")->item(0);
$div = $dom->createElement("div");
$dom->appendChild($div)->appendChild($form);
echo $dom->saveHTML();
Script 2.
$dom = new DOMDocument();
$div = $dom->createElement("div");
$dom->loadHTMLFile("html/signinform.html");//loads file here
$form = $dom->getElementsByTagName("form")->item(0);
$dom->appendChild($div)->appendChild($form);
echo $dom->saveHTML();
Script 1 works without problem. It shows the form. However Script 2 throws the following error: Fatal error: Uncaught exception 'DOMException' with message 'Wrong Document Error' in C:\Users
Could someone explain to me why the mere changing of position of the loadHTMLFile function results in such error? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已将一个元素添加到 DOM (
div
),然后尝试加载要解析的文件及其使用的 DOM 结构。如果您打算使用该文件,请先加载该文件。
You have added an element to the DOM (
div
) and then attempted to load a file to be parsed and its DOM structure used.Load the file first if you intend to use one.
对于 DOM 操作,您不需要插入已经存在的元素,因此执行如下操作:
$dom->appendChild($form)
仅当您使用 < 拉出元素时重新插入相同的表单元素code>$dom->getElementsByTag("form")->item(0) 它成为它自己的 DOM 对象,您可以直接引用并附加到该对象。一个合适的例子是:应该直接附加到他们从 DOM 中提取的对象,然后首先加载文档。
为了帮助解决您最初的问题:
new DOMDocument
可用于创建多个文档。loadHTMLFile
之前使用DOMDocument::createElement
创建 2 个DOMDocuments
。DomDocument::createDocumentFragment
的行为相同并创建它自己的 DOM。如果您希望保持代码相同并创建两个
DomDocuments
,那么您应该使用DomDocument::importNode
,示例如下:For DOM manipulation you do not need to insert an already existing element so doing something like this:
$dom->appendChild($form)
only reinserts the same form element, when you pull an element using$dom->getElementsByTag("form")->item(0)
it becomes it's own DOM object which you can reference directly and append to. A proper example would be:One should append directly to the object they pulled from the DOM instead and load the document first.
To help aid your initial questions too:
new DOMDocument
can be used to create multiple documents.DOMDocument::createElement
beforeloadHTMLFile
creates 2DOMDocuments
.DomDocument::createDocumentFragment
acts the same and creates it's own DOM.If you would like to keep your code the same and create two
DomDocuments
then you should useDomDocument::importNode
, an example of this would be: