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