Estoy tratando de combinar múltiples XML en un archivo XML esqueleto para crear un nuevo archivo XML. Estoy tratando de crear un bucle para incluir múltiples XMLs y Combinar en un xml. Estoy intentando algo como esto, pero no está funcionando: dir File = new File ( "E: \ temp \"); File [] RootFiles = dir.listFiles (); Hasta ahora no soy capaz de crear XML correcta sola con XML. Cualquier ayuda sería apreciada.
Aquí está mi código XML con la entrada individual:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
File file = new File("Result.xml");
String baseXml = "E:\\IntelliJ\\TryXml\\src\\blank.xml";
String inputXmls = "E:\\IntelliJ\\TryXml\\src\\input1.xml";
Document doc1 = db.parse(baseXml);
Document doc2 = db.parse(inputXmls);
NodeList nList = doc1.getElementsByTagName("Configs");
Element element = (Element) nList.item(0);
Node copiedNode = doc2.importNode(element, true);
doc2.getDocumentElement().appendChild(copiedNode);
processXml(doc2, file);
}
private static void processXml(Document xml, File file) throws TransformerException {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.INDENT,"yes");
tf.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
DOMSource source = new DOMSource(xml);
StreamResult result = new StreamResult(file);
tf.transform(source,result);
}
Los archivos XML
Input1.xml
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<Configs>
<Config name="test1">
<title>Title 1</title>
<author>Author1</author>
</Config>
<Config name="test2">
<title>Title 2</title>
<author>Author2</author>
</Config>
</Configs>
<optional>I dont want this to be copied</optional>
<Ratings>
<body>
<Items name = "object 1">
<something1>something1</something1>
</Items>
</body>
</Ratings>
</rss>
Input2.xml
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<Configs>
<Config name="test3">
<title>Title 3</title>
<author>Author3</author>
</Config>
<Config name="test4">
<title>Title 4</title>
<author>Author4</author>
</Config>
</Configs>
<optional>I dont want this to be copied</optional>
<Ratings>
<body>
<Items name = "object 2">
<something1>something2</something1>
</Items>
</body>
</Ratings>
</rss>
blank.xml (xml esqueleto donde estaré insertando los elementos de acuerdo con las etiquetas)
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<Configs>
</Configs>
<Ratings>
<body>
</body>
</Ratings>
</rss>
Esto es lo que estoy haciendo
result.xml
<rss version="2.0">
<Configs>
<Config name="test1">
<title>Title 1</title>
<author>Author1</author>
</Config>
<Config name="test2">
<title>Title 2</title>
<author>Author2</author>
</Config>
</Configs>
<optional>I dont want this to be copied</optional>
<Ratings>
<body>
<Items name="object 1">
<something1>something1</something1>
</Items>
</body>
</Ratings>
<Configs>
</Configs>
</rss>
Esto es lo que necesito
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<Configs>
<Config name="test1">
<title>Title 1</title>
<author>Author1</author>
</Config>
<Config name="test2">
<title>Title 2</title>
<author>Author2</author>
</Config>
<Config name="test3">
<title>Title 3</title>
<author>Author3</author>
</Config>
<Config name="test4">
<title>Title 4</title>
<author>Author4</author>
</Config>
</Configs>
<Ratings>
<body>
<Items name="object 1">
<something1>something1</something1>
</Items>
<Items name="object 2">
<something1>something2</something1>
</Items>
</body>
</Ratings>
</rss>
Actualización Esto es lo que tengo sin el valor actualizado entre la etiqueta.
Así es como he definido los artículos como NodeList
NodeList itemsNodeList = inputDoc.getElementsByTagName("Items");
<rss version="2.0">
<Configs>
<Config name="test1">
<title>Title 1</title>
<author>Author1</author>
</Config>
<Config name="test2">
<title>Title 2</title>
<author>Author2</author>
</Config>
<Config name="test3">
<title>Title 3</title>
<author>Author3</author>
</Config>
<Config name="test4">
<title>Title 4</title>
<author>Author4</author>
</Config>
</Configs>
<Ratings>
<body>
<Items name="object 1">
<something1>something1</something1>
</Items>
<Items name="object 2">
<something1>something2</something1>
</Items>
</body>
</Ratings>
</rss>
solución de Java:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
File file = new File("Result.xml");
String baseXml = "blank.xml";
String[] inputXmls = {"input1.xml","input2.xml"};
Document resultDoc = db.parse(baseXml);
Node resultConfigsNode = resultDoc.getElementsByTagName("Configs").item(0);
Node resultRatingsBodyNode = resultDoc.getElementsByTagName("body").item(0);
for (String inputXml : inputXmls){
Document inputDoc = db.parse(inputXml);
NodeList configNodeList = inputDoc.getElementsByTagName("Config");
for (int i = 0; i < configNodeList.getLength(); i++) {
Node copiedNode = resultDoc.importNode(configNodeList.item(i), true);
resultConfigsNode.appendChild(copiedNode);
}
for (int i = 0; i < itemsNodeList.getLength(); i++) {
Node copiedNode = resultDoc.importNode(itemsNodeList.item(i), true);
NamedNodeMap attrMap = copiedNode.getAttributes();
Node n = attrMap.getNamedItem("name");
if(n.getNodeValue().equals("object 1")){
System.out.println("Items object 1");
}
resultRatingsBodyNode.appendChild(copiedNode);
}
NodeList valueNodeList = inputDoc.getElementsByTagName("value");
for (int i = 0; i < valueNodeList.getLength(); i++) {
Node copiedNode = resultDoc.importNode(valueNodeList.item(i), true);
Text txt = (Text) copiedNode.getFirstChild();
txt.setData("NewValue");
resultRatingsBodyNode.appendChild(copiedNode);
}
}
processXml(resultDoc, file);