Xml Fusion xmls multiples dans un nouveau fichier Xml

404or505:

Je suis en train de fusionner plusieurs xml dans un fichier xml squelette pour créer un nouveau fichier XML. Je suis en train de créer une boucle pour inclure plusieurs xmls et fusionner en un seul xml. J'essaie quelque chose comme ça, mais il ne fonctionne pas: fichier dir = new File ( "E: \ temp \"); Fichier [] = rootFiles dir.listFiles (); Jusqu'à présent, je ne suis pas en mesure de créer xml correct avec XML. Toute aide serait appréciée.

Voici mon code avec entrée unique xml:

 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);
}

fichiers 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 squelette où je serai insertion, les éléments d'après les tags)

<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
    <Configs>

    </Configs>
    <Ratings>
        <body>

        </body>
    </Ratings>
</rss>

C'est ce que je reçois

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>

C'est de quoi j'ai besoin

 <?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>

Mise à jour ce que je suis sans la valeur actualisée entre l'étiquette.

Voici comment je définissais les articles comme 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>
Sergey Bzhezitskiy:

solution 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);

Je suppose que tu aimes

Origine http://43.154.161.224:23101/article/api/json?id=336101&siteId=1
conseillé
Classement