gpt4 book ai didi

java - 将 XML 文件中的子节点拆分为自己的 XML 文件

转载 作者:行者123 更新时间:2023-11-29 04:04:10 25 4
gpt4 key购买 nike

我有一个 XML 文件(左侧),我想创建多个文件(右侧):

<ParentNode>                                  file1:
<ChildNode> <ParentNode>
<node></node> <ChildNode>
</childNode> <node></node>
<ChildNode> </childNode>
<node></node> </ParentNode>
</childNode> file2:
<ChildNode> <ParentNode>
<node></node> <ChildNode>
</childNode> <node></node>
</ParentNode> </childNode>
</ParentNode>

我正在尝试从原始 XML 文件中取出第一个子节点并将其添加到一个新文件中,但在替换节点时我总是遇到错误。

我想做类似下面的事情

 DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document newDocument;

Node firstChild = document.getFirstChild();
NodeList childNodes = firstChild.getChildNodes();

Element parentNode;
for (int i = 1; i < childNodes.getLength(); i++ ) {
newDocument = docBuilder.newDocument();
parentNode = newDocument.createElement("ParentNode");
newDocument.appendChild(parentNode);
newDocument.getFirstChild().appendChild(childNodes.item(i));
}

但是我得到一个错误

org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.

感谢任何指向正确方向的帮助!

最佳答案

来自 Java 文档,

使用cloneNode方法。

SUMMARY:

public Node cloneNode(boolean deep)

Returns a duplicate of this node, i.e., serves as a generic copy constructor for nodes. The duplicate node has no parent; ( parentNode is null.).

Cloning an Element copies all attributes and their values, including those generated by the XML processor to represent defaulted attributes, but this method does not copy any text it contains unless it is a deep clone, since the text is contained in a child Text node. Cloning an Attribute directly, as opposed to be cloned as part of an Element cloning operation, returns a specified attribute ( specified is true). Cloning any other type of node simply returns a copy of this node.

Note that cloning an immutable subtree results in a mutable copy, but the children of an EntityReference clone are readonly . In addition, clones of unspecified Attr nodes are specified. And, cloning Document, DocumentType, Entity, and Notation nodes is implementation dependent.

编辑:

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class Test{
static public void main(String[] arg) throws Exception{

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("foo.xml");

TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();


NodeList list = doc.getFirstChild().getChildNodes();

for (int i=0; i<list.getLength(); i++){
Node element = list.item(i).cloneNode(true);

if(element.hasChildNodes()){
Source src = new DOMSource(element);
FileOutputStream fs=new FileOutputStream("k" + i + ".xml");
Result dest = new StreamResult(fs);
aTransformer.transform(src, dest);
fs.close();
}
}

}
}

关于java - 将 XML 文件中的子节点拆分为自己的 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1372771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com