JDOM不允许同一个节点同时被2个或多个文档相关联,要在第2个文档中使用原来老文档中的节点的话。首先需要使用detach()把这个节点分开来。
//读取文件: StringReader sr = new StringReader(requestXml); InputSource is = new InputSource(sr); SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(is);
//输出元素: Document newXmlDoc = new Document(newXmlRoot); XMLOutputter output = new XMLOutputter(); String newXml = output.outputString(newXmlDoc);
//添加元素 root.addContent(new Element("jane"));
//获取元素: Namespace soapenv = Namespace.getNamespace("soapenv",SOAP_URI); Namespace put = Namespace. getNamespace("put",PUT_URI); Element root = doc.getRootElement(); Element bodyElement = root.getChild( BODY,soapenv); Element inbound = bodyElement.getChild("DisassembleJobcardFeedback",put).getChild( "DisassembleJobcardFeedbackInfo" );