合并xml时删除重复节点并更新属性名称

时间:2019-05-22 17:55:59

标签: java xml jaxb

在合并xml时,我想忽略重复的节点,如果属性值有任何变化,我想对其进行更新。

我提到的Xml Merge Multiple Xmls into a new Xml file并没有回答我的问题。

Base.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <rss version="2.0">
    <Configs>
        <default name="default">
            <something>default</something>
        </default>

    </Configs>
    <Ratings>
        <body>

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

Input1.xml

<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
    <Configs>
        <default name="default">
            <something>default</something>
        </default>
        <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>
        <default name="default">
            <something>default</something>
        </default>
        <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>

预期-> Result.xml

<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
    <Configs>
        <default name="default">
            <something>default</something>
        </default>
        <Config name="test1">
            <title>Title 1</title>
            <author>Author1</author>
        </Config>
        <Config name="test2">
            <title>Title 2</title>
            <author>Author2</author>
        </Config>
    </Configs>
    <Ratings>
        <body>
            <Items name = "object 1">
                <something1>something1</something1>
            </Items>
            <Items name = "object 2">
                <something1>this is updated value</something1>
            </Items>
        </body>
    </Ratings>
</rss>

此处,以下内容已更新:

<Items name = "object 2">
                    <something1>this is updated value</something1>
                </Items>

要删除重复的节点,我尝试使用

Document inputDoc = db.parse(inputFile(s));

        NodeList configNodeList = inputDoc.getElementsByTagName("Configs");

        for (int i = 0; i < configNodeList.getLength(); i++) {
        Node copiedNode = resultDoc.importNode(configNodeList.item(i), true);
        NamedNodeMap attributes = copiedNode.getAttributes();
        attributes.removeNamedItem("<default name=\"default\">");
        resultConfigsNode.appendChild(copiedNode);
        }

更新:

我能够删除/忽略重复的节点

String removeDuplicate = "name=\"default\"";

                if (attributes.item(0).toString().equalsIgnoreCase(removeDuplicate)) {
                    continue;
                }

现在只需要帮助即可更新以下内容

<Items name = "object 2">
                <something1>this is updated value</something1>

0 个答案:

没有答案