新しいXMLファイルに複数のXMLSをマージするXML

404or505:

私は、新しいXMLファイルを作成するために、スケルトンXMLファイルに複数のXMLをマージしようとしています。私は1つのXMLに複数のXMLSとのマージを含むようにループを作成しようとしています。私はこのような何かをしようとしていますが、それは動作しません:ファイルDIR =新しいファイル(「E:\ TEMP \」); ファイル[] rootFiles = dir.listFiles()。これまでのところ私は、単一のXMLで正しいXMLを作成することはできませんよ。任意の助けいただければ幸いです。

ここでは、単一の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);
}

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(I、タグに応じて要素を挿入するスケルトンXML)

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

    </Configs>
    <Ratings>
        <body>

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

これは私が取得していますものです

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>

これが私が必要なものです

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

更新これは私がタグの間に更新された値なし得たものです。

これは私がの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>
セルゲイBzhezitskiy:

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

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=336100&siteId=1