使用Java将Node插入XML的根元素的第一个子元素中

时间:2019-01-18 08:53:13

标签: xml dom document xmlunit

Comparing the Source XML with Destination XML and want to add the missing tags to the Destination XML.

In this case tag <abcd></abcd> is missing from the Destination XML. But when I tried to import this specific node to the Destination node result is Merged XML which is not as per my expectation. Expected needs to be identical to Source XML.

Source XML

    <Adapter>
        <Module id="BL">
            <gl>Y</gl>
            <glActivitySeries>AC</glActivitySeries>
            <target>FIC.BL</target>
            <abcd></abcd>
        </Module>
    </Adapter>

Destination XML

        <Adapter>
            <Module id="BL">
                <gl>Y</gl>
                <glActivitySeries>AC</glActivitySeries>
                <target>FIC.BL</target>
            </Module>
        </Adapter>

Merged XML 

            <Adapter>
                <Module id="BL">
                    <gl>Y</gl>
                    <glActivitySeries>AC</glActivitySeries>
                    <target>FIC.BL</target>
                </Module>
                <abcd/>
            </Adapter>

我的Java代码

获取源XML中缺少的节点

NodeList tempNodeList=node.getElementsByTagName("abcd");
tempNodeList.item(0);

将该节点导入到目标XML

Node newTempNode = destDocument.importNode(requiredNode,true);               
destDocument.getDocumentElement().appendChild(newTempNode);

代码中的错误是什么导致我的输出不符合我的预期。任何领先的企业都可以。

0 个答案:

没有答案