gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:50:24 24 4
gpt4 key购买 nike

我想将里面的元素附加到另一个带有标签的 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(在序列标记内>

空白.xml

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

这就是我所期待的结果.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>

更新如果我有以下空白.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>

最佳答案

仅处理子节点。

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

关于java - 使用java复制节点内的所有元素,但不复制XML中的节点标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56278541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com