-1
我想從選擇節點創建新的xml文件只有我使用dom4j解析並創建新的xml文件。例如讓我們假設節點客戶名稱=約瑟夫是根元素TRX的孩子,我想證明含有約瑟夫的所有元素,並創建一個新的文件我想搜索xml中的特定節點或元素,並從選擇節點創建新的xml
enter code here
File inputFile = new File("C:\\Users\\db2admin\\Desktop\\S4decs\\tlog01_004.xml");
SAXReader reader = new SAXReader(true);
reader.setValidation(false);
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
Document document = reader.read(inputFile);
document.getRootElement();
document.selectNodes("//TRX[@type]='16'").size();
document.selectNodes("/CUSTOMER").size();
// Pretty print the document to System.out
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer;
writer = new XMLWriter(System.out, format);
writer.write(document);
} catch (DocumentException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE TRANSACTIONS SYSTEM "tlog.dtd">
<TRANSACTIONS storeid="4" sbs="780030" location="1">
<TRX type="16" term="742" trxnum="143895" saleperson="0" supervisor_id="152332149" storeid="4" sbs="780030" opcode="153135959" date="20160915" endtime="111000">
</TRX>
<TRX type="31" term="742" trxnum="143896" starttime="095720" supervisor_id="152332149" storeid="4" sbs="780030" opcode="153135959" date="20160915" endtime="111001">
<CASHOPER managerid="153135959">
<PAYMENT id="1" amount="3000.00" descr="CASH" tndnumb="1" exchangetndid="0">
</PAYMENT>
</CASHOPER>
<LINKTRX linktype="8" prevstoreid="4" prevxactdate="2016-09-14" prevxacttime="22:58:29" prevtermid="0" prevxactid="7620" prevoperid="0"></LINKTRX>
</TRX>
<TRX type="16" term="743" trxnum="65729" saleperson="0" supervisor_id="153136068" storeid="4" sbs="780030" opcode="152332262" date="20160915" endtime="111219">
</TRX>
問題當我運行這個仍然顯示整個Xml文件 –
你沒有對文檔做任何改變。創建一個新文檔,並將打開的文檔中的選定節點添加到新文檔。請參閱:http://dom4j.sourceforge.net/dom4j-1.6.1/guide.html#Creating_a_new_XML_document –
感謝Sir F.Klein –