您在正確的軌道上創建新文檔,解析零件並將其添加到新零件中。
您接近失敗,因爲您嘗試將整個文檔追加到另一個不可能的地方。
你可以嘗試這樣的事:
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;
}
以上副本的代碼每個文檔的根元素下找到的所有節點。當然,您可以選擇僅複製您感興趣的節點。生成的文檔將反映這兩個文檔中的所有節點。
測試
@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>
編輯
我想創建一個 「假」 的父母,串聯的文件,BU T,然後只能收回元素的列表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]
是'headerTag'的根元素嗎?如果不是什麼是根元素? – A4L 2014-10-31 18:51:19
您需要一個根元素來構建適當的xml文檔。如果它不是xml,請提及構建它的標記語言。此外,爲什麼不將org.w3c.Document轉換爲各自的String文件並將它們連接起來,最後再創建另一個org.w3c.Document實例。 – 2014-10-31 18:52:11
@ A4L,'headerTag'是由方法'createDocument1'和'createDocument2'創建的文檔的根元素,我唯一想要的是將一個接一個地連接起來,即使我必須創建一個根父級。 – Manuelarte 2014-10-31 19:04:36