使用Java复制节点内的所有元素,但不复制XML中的节点标签

时间:2019-05-23 15:27:57

标签: java xml

我想将内部元素附加到带有标签的另一个xml中。我不想复制标签。

我正在执行以下操作,但它也会复制节点名称()。

NodeList itemsNodeList = inputDoc.getElementsByTagName("Sequence");

            for (int i = 0; i < itemsNodeList.getLength(); i++) {
                Node copiedNode = resultDoc.importNode(itemsNodeList.item(i), true);
                resultRatingsBodyNode.appendChild(copiedNode);
            }

这些是我要从中复制的xml(序列标记中的所有内容)

xml1.xml

<?xml version="1.0" encoding="UTF-8"?>
<processes>
    <process>
        <body name="main">
            <sequence>
                <receive name="Receive1" createInstance="yes" />
                <assign name="Assign1" />
                <invoke name="Invoke1" />
                <style>
                    <Font>someFont</Font>
                </style>
                <assign name="Assign2" />
                <reply name="Reply1" />
            </sequence>
        </body>
    </process>
</processes>

xml2.xml

<?xml version="1.0" encoding="UTF-8"?>
<processes>
    <process>
        <body name="main">
            <sequence>
                <receive name="Receive1" createInstance="yes" />
                <assign name="Assign2" />
                <invoke name="Invoke2" />
                <style>
                    <Font>someFont1</Font>
                </style>
                <assign name="Assign3" />
                <reply name="Reply2" />
            </sequence>
        </body>
    </process>
</processes>

这是我要复制到的xml(在序列标签内)

blank.xml

<?xml version="1.0" encoding="UTF-8"?>
<processes>
    <process>
        <body name="main">
            <sequence>              
            </sequence>
        </body>
    </process>
</processes>

这是我所期望的 Result.xml

<?xml version="1.0" encoding="UTF-8"?>
<processes>
    <process>
        <body name="main">
            <sequence>
                <receive name="Receive1" createInstance="yes" />
                <assign name="Assign1" />
                <invoke name="Invoke1" />
                <style>
                    <Font>someFont</Font>
                </style>
                <assign name="Assign2" />
                <reply name="Reply1" />
                <receive name="Receive2" createInstance="yes" />
                <assign name="Assign2" />
                <invoke name="Invoke2" />
                <style>
                    <Font>someFont1</Font>
                </style>
                <assign name="Assign3" />
                <reply name="Reply2" />
            </sequence>
        </body>
    </process>
</processes>

更新 如果我有follwing.xml,该如何在将元素复制到序列节点之前删除序列节点中的元素?

<?xml version="1.0" encoding="UTF-8"?>
<processes>
    <process>
        <body name="main">
            <sequence> 
               <delete>Delete everthing between sequence tag before copying</delete>
            </sequence>
        </body>
    </process>
</processes>

更新2

我得到以下信息。我认为它是在复制之前删除节点内的所有内容。

<?xml version="1.0" encoding="UTF-8"?>
<processes>
    <process>
        <body name="main">
            <sequence>
                <receive name="Receive2" createInstance="yes" />
                <assign name="Assign2" />
                <invoke name="Invoke2" />
                <style>
                    <Font>someFont1</Font>
                </style>
                <assign name="Assign3" />
                <reply name="Reply2" />
            </sequence>
        </body>
    </process>
</processes>

1 个答案:

答案 0 :(得分:2)

仅处理childNodes。

NodeList itemsNodeList = inputDoc.getElementsByTagName("sequence");
Node resultSequence= resultDoc.getElementsByTagName("sequence").item(0);

NodeList resultChildren=resultSequence.getChildNodes();
for(int i=0;i<resultChildren.getLength();i++) {
    resultSequence.removeChild(resultChildren.item(i));
}

for (int i = 0; i < itemsNodeList.getLength(); i++) {
    NodeList children=itemsNodeList.item(i).getChildNodes();
    for(int j=0;j<children.getLength();j++) {
        Node copiedNode = resultDoc.importNode(children.item(j), true);
        resultSequence.appendChild(copiedNode);
    }
}