InputStream is = new FileInputStream("D://dom4j.xml");SAXReader reader = new SAXReader();Document doc = reader.read(is);doc.normalize();Node node = doc.selectSingleNode("/datas/*");//查詢datas下的第一個子結點的內空node.getParent().remove(node);OutputFormat outformat = OutputFormat.createPrettyPrint();// xml自身的編碼outformat.setEncoding(doc.getXMLEncoding());outformat.setIndent(true);File targetFile = new File("D:/dom4j3.xml");// 將xmlStr輸出到目標路徑,使用xml檔案的標頭檔案編碼OutputStream out = new FileOutputStream(targetFile);XMLWriter writer = new XMLWriter(out, outformat);// 是否轉義特殊字元,預設true轉義,false表示不轉義writer.setEscapeText(false);writer.write(doc);writer.flush();
InputStream is = new FileInputStream("D://dom4j.xml");SAXReader reader = new SAXReader();Document doc = reader.read(is);doc.normalize();Node node = doc.selectSingleNode("/datas/*");//查詢datas下的第一個子結點的內空node.getParent().remove(node);OutputFormat outformat = OutputFormat.createPrettyPrint();// xml自身的編碼outformat.setEncoding(doc.getXMLEncoding());outformat.setIndent(true);File targetFile = new File("D:/dom4j3.xml");// 將xmlStr輸出到目標路徑,使用xml檔案的標頭檔案編碼OutputStream out = new FileOutputStream(targetFile);XMLWriter writer = new XMLWriter(out, outformat);// 是否轉義特殊字元,預設true轉義,false表示不轉義writer.setEscapeText(false);writer.write(doc);writer.flush();