gpt4 book ai didi

java - 连接两个 org.w3c.dom.Document

转载 作者:行者123 更新时间:2023-12-01 22:37:31 24 4
gpt4 key购买 nike

我想连接两个 org.w3c.dom.Document ,我有这样的东西:

Document finalDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
Document document1 = createDocumentOne();
Document document2 = createDocumentTwo();

// This didn't work
changeFileDocument.appendChild(document1);
changeFileDocument.appendChild(document2);

document1和document2的格式是这样的:

<headerTag>
<tag1>value</tag1>
</headerTag>

最后我想要的是这样的文档:

<headerTag>
<tag1>valueForDocument1</tag1>
</headerTag>
<headerTag>
<tag1>valueForDocument2</tag1>
</headerTag>

我认为你不能这样做,因为他们应该有一个共同的 parent 。如果是这样,我想创建那个“假”父级,连接文件,但然后只恢复元素列表 headerTag

我该怎么做?

最佳答案

创建新文档、解析各个部分并将其节点添加到新文档中,您的方向是正确的。

您的方法失败了,因为您尝试将整个文档附加到另一个文档,这是不可能的。

你可以尝试这样的事情:

public org.w3c.dom.Document concatXmlDocuments(String rootElementName, InputStream... xmlInputStreams) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document result = builder.newDocument();
org.w3c.dom.Element rootElement = result.createElement(rootElementName);
result.appendChild(rootElement);
for(InputStream is : xmlInputStreams) {
org.w3c.dom.Document document = builder.parse(is);
org.w3c.dom.Element root = document.getDocumentElement();
NodeList childNodes = root.getChildNodes();
for(int i = 0; i < childNodes.getLength(); i++) {
Node importNode = result.importNode(childNodes.item(i), true);
rootElement.appendChild(importNode);
}
}
return result;
}

上面的代码复制在每个文档的根元素下找到的所有节点。当然,您可以选择仅选择性地复制您感兴趣的节点。生成的文档将反射(reflect)两个文档中的所有节点。

测试

@Test
public void concatXmlDocuments() throws ParserConfigurationException, SAXException, IOException, TransformerException {
try (
InputStream doc1 = new ByteArrayInputStream((
"<headerTag>\r\n" +
" <tag1>doc1 value</tag1>\r\n" +
"</headerTag>").getBytes(StandardCharsets.UTF_8));
InputStream doc2 = new ByteArrayInputStream((
"<headerTag>\r\n" +
" <tag1>doc2 value</tag1>\r\n" +
"</headerTag>").getBytes(StandardCharsets.UTF_8));
ByteArrayOutputStream docR = new ByteArrayOutputStream();

) {

org.w3c.dom.Document result = concatXmlDocuments("headerTag", doc1, doc2);
TransformerFactory trf = TransformerFactory.newInstance();
Transformer tr = trf.newTransformer();
tr.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(result);
StreamResult sr = new StreamResult(docR);
tr.transform(source, sr);
System.out.print(new String(docR.toByteArray(), StandardCharsets.UTF_8));
}
}

输出

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<headerTag>
<tag1>doc1 value</tag1>
<tag1>doc2 value</tag1>
</headerTag>

编辑

I would like to create that "fake" parent, concatenate the files, but then only recover the List of elements headerTag

正如您所说,创建一个父级。以下是您可以如何做到的:

1) 进行串联

public org.w3c.dom.Document concatXmlDocuments(InputStream... xmlInputStreams) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document result = builder.newDocument();
org.w3c.dom.Element rootElement = result.createElement("fake");
result.appendChild(rootElement);
for(InputStream is : xmlInputStreams) {
org.w3c.dom.Document document = builder.parse(is);
org.w3c.dom.Element subRoot = document.getDocumentElement();
Node importNode = result.importNode(subRoot, true);
rootElement.appendChild(importNode);
}
return result;
}

2)恢复headerTag的节点列表

public NodeList recoverTheListOfElementsHeaderTag(String xml) throws ParserConfigurationException, SAXException, IOException {
NodeList listOfElementsHeaderTag = null;
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
try (InputStream is = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))) {
listOfElementsHeaderTag = recoverTheListOfElementsHeaderTag(builder.parse(is));
}
return listOfElementsHeaderTag;
}

public NodeList recoverTheListOfElementsHeaderTag(org.w3c.dom.Document doc) {
org.w3c.dom.Element root = doc.getDocumentElement();
return root.getChildNodes();
}

测试

@Test
public void concatXmlDocuments() throws ParserConfigurationException, SAXException, IOException, TransformerException {
try (
InputStream doc1 = new ByteArrayInputStream((
"<headerTag>" +
"<tag1>doc1 value</tag1>" +
"</headerTag>").getBytes(StandardCharsets.UTF_8));
InputStream doc2 = new ByteArrayInputStream((
"<headerTag>" +
"<tag1>doc2 value</tag1>" +
"</headerTag>").getBytes(StandardCharsets.UTF_8));

) {

org.w3c.dom.Document result = concatXmlDocuments(doc1, doc2);
String resultXML = toXML(result);
System.out.printf("%s%n", resultXML);
NodeList listOfElementsHeaderTag = null;
System.out.printf("===================================================%n");
listOfElementsHeaderTag = recoverTheListOfElementsHeaderTag(resultXML);
printNodeList(listOfElementsHeaderTag);
System.out.printf("===================================================%n");
listOfElementsHeaderTag = recoverTheListOfElementsHeaderTag(result);
printNodeList(listOfElementsHeaderTag);
}
}


private String toXML(org.w3c.dom.Document result) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException, IOException {
String resultXML = null;
try (ByteArrayOutputStream docR = new ByteArrayOutputStream()) {
TransformerFactory trf = TransformerFactory.newInstance();
Transformer tr = trf.newTransformer();
DOMSource source = new DOMSource(result);
StreamResult sr = new StreamResult(docR);
tr.transform(source, sr);
resultXML = new String(docR.toByteArray(), StandardCharsets.UTF_8);
}
return resultXML;
}

private void printNodeList(NodeList nodeList) {
for(int i = 0; i < nodeList.getLength(); i++) {
printNode(nodeList.item(i), "");
}
}

private void printNode(Node node, String startIndent) {
if(node != null) {
System.out.printf("%s%s%n", startIndent, node.toString());
NodeList childNodes = node.getChildNodes();
for(int i = 0; i < childNodes.getLength(); i++) {
printNode(childNodes.item(i), startIndent+ " ");
}
}
}

输出

<?xml version="1.0" encoding="UTF-8" standalone="no"?><fake><headerTag><tag1>doc1 value</tag1></headerTag><headerTag><tag1>doc2 value</tag1></headerTag></fake>
===================================================
[headerTag: null]
[tag1: null]
[#text: doc1 value]
[headerTag: null]
[tag1: null]
[#text: doc2 value]
===================================================
[headerTag: null]
[tag1: null]
[#text: doc1 value]
[headerTag: null]
[tag1: null]
[#text: doc2 value]

关于java - 连接两个 org.w3c.dom.Document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26681471/

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