[英]What's wrong with this PHP DOM code?
$doc = new DOMDocument();
$doc -> load("state.xml");
...snip...
$neworder = $doc -> createElement("order");
$neworder -> appendChild($type);
That last line of code is causing the script to fail. 最后一行代码导致脚本失败。 500 HTTP response.
500 HTTP响应。 If I comment it out, the script runs fine.
如果我将其注释掉,则脚本可以正常运行。
get_class() confirms that both $neworder and $type are of the DOMElement class. get_class()确认$ neworder和$ type都属于DOMElement类。 What could the problem be?
可能是什么问题?
... ...
On where I'm getting the nodes I'm trying to add: 在要添加节点的位置:
This is where $type comes from. 这是$ type的来源。
for ($i = 0; $i < $ordersummary -> length; $i++)
{
$o = $ordersummary -> item($i);
$type = $o -> getElementsByTagName("type") -> item(0);
...
and $ordersummary is obtained like this 和$ ordersummary这样获得
$ordersummary -> loadXML($_POST["data"]);
$ordersummary = $ordersummary -> getElementsByTagName("order");
The Exception is thrown because you try to append a node from one Document into another. 引发异常是因为您尝试将一个文档中的节点附加到另一个文档中。 That is not supported by the DOM API.
DOM API不支持。
EDIT: The only sane workaround is to deep-copy the element. 编辑:唯一理智的解决方法是深度复制元素。 Depending on whether or not type is a complex XML structure it may be just a lot easier to serialise the type node to XML string, then create a DOMDocumentFragment for the receiving DOMDocument object and load the raw XML into this DOMDocumentFramgnent.
根据类型是否为复杂的XML结构,将类型节点序列化为XML字符串,然后为接收的DOMDocument对象创建DOMDocumentFragment并将原始XML加载到此DOMDocumentFramgnent中可能要容易得多。 Then you can append the DOMDocumentFragment as you would a DOMElement to insert the type XML data.
然后,您可以像添加DOMElement一样添加DOMDocumentFragment来插入类型XML数据。
As jswolf19 pointed out, you can't append a node coming from a different document, from the documentation: 正如jswolf19所指出的,您不能在文档中附加来自其他文档的节点:
DOM_WRONG_DOCUMENT_ERR
DOM_WRONG_DOCUMENT_ERR
Raised if newnode was created from a different document than the one that created this node.
如果newnode是从与创建该节点的文档不同的文档创建的,则引发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.