日期:2014-05-20 浏览次数:20691 次
<?xml version="1.0" encoding="UTF-8"?>
<explogs xmlns="http://java.sun.com/xml/ns/javaee">
<logs xmlns="" id="1">
<content>张三</content>
<content>李四</content>
<content>王五</content>
</logs>
<logs xmlns="" id="2">
<content>小黑</content>
<content>小红</content>
<content>小白</content>
</logs>
</explogs>
<?xml version="1.0" encoding="UTF-8"?>
<explogs xmlns="http://java.sun.com/xml/ns/javaee">
<logs xmlns="" id="1">
<content>张三</content>
<content>李四</content>
<content>王五</content>
<content>此为追加的节点</content>
</logs>
<logs xmlns="" id="2">
<content>小黑</content>
<content>小红</content>
<content>小白</content>
</logs>
</explogs>
org.jdom.IllegalAddException: The Content already has an existing parent "explogs"
SAXBuilder builder = new SAXBuilder();
Document xmlJDoc = builder.build(new File("c:\\test.xml"));
Element rootElement = xmlJDoc.getRootElement();
Element logElement = (Element)XPath.selectSingleNode(rootElement, "//explogs/logs[@id=1]");
logElement.addContent(new Element("content").addContent("追加的内容"));
System.out.println(XMLHelper.toXMLString(rootElement));
logsElement.addContent(logsContentElement.detach())
<?xml version="1.0" encoding="UTF-8"?>
<explogs xmlns="http://java.sun.com/xml/ns/javaee">
<logs xmlns="" id="1">
<content>张三</content>