解决Dom4j中"The Node already has an existing parent"问题

在xml开发中经常会需要将一个Document的部分元素复制到另一个Document中,但dom4j中直接add会产生“The Node already has an existing parent”异常。
下面是我的示例,要将image节点合成如下文档形式。

<?xml version="1.0" encoding="UTF-8"?>
<data>
<deviceId>device006</deviceId>
<deviceType>QDZYDP</deviceType>
<images>
<image id="0" name="device006_1.png" width="240" height="144">
<lines>
<line id="1" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="2" >
<subLines>
<subLine lineType="1">
<beginPoint x="27" y="36.5"/>
<endPoint x="205" y="36.5"/>
</subLine>
</subLines>
</line>
<line id="2" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="1" >
<subLines>
<subLine lineType="1">
<beginPoint x="27" y="56.5"/>
<endPoint x="84" y="56.5"/>
</subLine>
</subLines>
</line>
<line id="3" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="1" >
<subLines>
<subLine lineType="1">
<beginPoint x="83" y="56"/>
<endPoint x="111" y="43"/>
</subLine>
</subLines>
</line>
<line id="4" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="2" >
<subLines>
<subLine lineType="1">
<beginPoint x="27" y="109.5"/>
<endPoint x="205" y="109.5"/>
</subLine>
</subLines>
</line>
<line id="5" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="2" >
<subLines>
<subLine lineType="1">
<beginPoint x="34.5" y="29"/>
<endPoint x="34.5" y="117"/>
</subLine>
</subLines>
</line>
<line id="6" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="3" >
<subLines>
<subLine lineType="1">
<beginPoint x="112.5" y="29"/>
<endPoint x="112.5" y="117"/>
</subLine>
</subLines>
</line>
<line id="7" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="2" >
<subLines>
<subLine lineType="1">
<beginPoint x="197.5" y="29"/>
<endPoint x="197.5" y="117"/>
</subLine>
</subLines>
</line>
</lines>
</image>
<image id="3" name="device006_2.png" width="240" height="144">
<lines>

<line id="1" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="2" >
<subLines>
<subLine lineType="1">
<beginPoint x="127" y="79"/>
<endPoint x="162" y="115"/>
</subLine>
</subLines>
</line>
<line id="2" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="1" >
<subLines>
<subLine lineType="1">
<beginPoint x="11" y="50.5"/>
<endPoint x="98" y="50.5"/>
</subLine>
</subLines>
</line>
<line id="3" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="3" >
<subLines>
<subLine lineType="1">
<beginPoint x="122" y="34.5"/>
<endPoint x="172" y="34.5"/>
</subLine>
</subLines>
</line>
<line id="4" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="2" >
<subLines>
<subLine lineType="1">
<beginPoint x="11" y="78.5"/>
<endPoint x="172" y="78.5"/>
</subLine>
</subLines>
</line>
<line id="5" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="1" >
<subLines>
<subLine lineType="1">
<beginPoint x="11" y="113.5"/>
<endPoint x="172" y="113.5"/>
</subLine>
</subLines>
</line>
<line id="6" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="3" >
<subLines>
<subLine lineType="1">
<beginPoint x="18.5" y="43"/>
<endPoint x="18.5" y="125"/>
</subLine>
</subLines>
</line>
<line id="7" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="3" >
<subLines>
<subLine lineType="1">
<beginPoint x="125.5" y="27"/>
<endPoint x="125.5" y="78"/>
</subLine>
</subLines>
</line>
<line id="8" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="3" >
<subLines>
<subLine lineType="1">
<beginPoint x="164.5" y="27"/>
<endPoint x="164.5" y="121"/>
</subLine>
</subLines>
</line> <line id="9" lineType="1" lineWidth="8" roadId="" begin="0" end="0" color="2" >
<subLines>
<subLine lineType="1">
<beginPoint x="96" y="50"/>
<endPoint x="125" y="33"/>
</subLine>
</subLines>
</line>
</lines>
</image>
</images>
</data>


public static String addImageNode(String xml1, String xml2, int id) {
if (xml1 != null && !xml1.equals("")) {
Document document1 = getUTFDocument(xml1);
Document document2 = getUTFDocument(xml2);
List dataNode = document1.selectNodes("/data/images");
Element e = (Element) dataNode.get(0);
List imagelist = document2.selectNodes("/data/images/image");
Element e2 = (Element) imagelist.get(0);
Attribute ima = e2.attribute("id");
ima.setValue(String.valueOf(id));
// dataNode.add(e2);

e.add((Element)e2.clone());
[color=red]// e.addElement(e2.getName()).appendContent(e2); [/color]
return outPutXML(document1);
} else {
Document document2 = getUTFDocument(xml2);
List dataNode = document2.selectNodes("/data/images/image");
Element e = (Element) dataNode.get(0);
Attribute ima = e.attribute("id");
ima.setValue(String.valueOf(id));
return outPutXML(document2);
}

}


红色部分也可以添加,但是会丢失属性。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值