往xml中更新節(jié)點(diǎn)的實例代碼
更新時間:2016年07月06日 11:04:40 投稿:jingxian
下面小編就為大家?guī)硪黄鵻ml中更新節(jié)點(diǎn)的實例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
往xml中更新節(jié)點(diǎn)的實例代碼
/* System.out.println("2323");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docbuilder = factory.newDocumentBuilder();
Document parse = docbuilder
.parse(new File("src/ProdQuery.xml"));
// Element createElement2 = parse.createElement("");
Element createElement = parse.createElement("products");
Node item2 = parse.getChildNodes().item(parse.getChildNodes().getLength()-1);
for (String item : map.keySet()) {
//循環(huán)添加至products
System.out.println("tianjia1");
Element clidren = parse.createElement("product");
Attr createAttribute = parse.createAttribute("name");
createAttribute.setNodeValue(item);
Attr createAttribute2 = parse.createAttribute("value");
createAttribute2.setNodeValue(map.get(item));
clidren.setAttributeNode(createAttribute);
clidren.setAttributeNode(createAttribute2);
createElement.appendChild(clidren);
}
//將products添加到根目錄
item2.appendChild(createElement);
//創(chuàng)建一個TransformerFactory對象
TransformerFactory tFactory = TransformerFactory.newInstance();
//得到一個操作對象
Transformer transformer = tFactory.newTransformer();
//設(shè)置們,這個是編碼
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
//
DOMSource source = new DOMSource(parse);
System.out.println("改變文件");
StreamResult result = new StreamResult(new File("src/ProdQuery.xml"));
//讓xml文件換行
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.transform(source, result);*/
以上這篇往xml中更新節(jié)點(diǎn)的實例代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
javascript Ajax獲取遠(yuǎn)程url的返回判斷
將以下文本放入一個HTML頁面即可看到效果,將會有兩次彈出提示,最后在頁面上顯示YES,表示完成2012-01-01
用ajax傳遞json到前臺中文出現(xiàn)問號亂碼問題的解決辦法
這篇文章主要介紹了用ajax傳遞json到前臺中文出現(xiàn)問號亂碼問題的解決辦法,需要的朋友參考下2017-01-01
基于Ajax技術(shù)實現(xiàn)考試倒計時并自動提交試卷
這篇文章主要介紹了基于Ajax技術(shù)實現(xiàn)考試倒計時并自動提交試卷 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-06-06
使用jquery 的ajax調(diào)用總是錯誤親測的解決方法
使用jquery 的ajax功能調(diào)用一個頁面,卻發(fā)現(xiàn)總是出現(xiàn)錯誤,經(jīng)過這么多測試終于正常了,尤其是 dataType: 'json',看來jquery有很嚴(yán)格的驗證機(jī)制2013-07-07
關(guān)于異步請求ajax原理以及原生Ajax、$.ajax的基本使用詳解
這篇文章主要介紹了關(guān)于異步請求ajax原理以及原生Ajax、$.ajax的基本使用詳解,ajax現(xiàn)在是前后端交互的重要工具,可以說,只要從事于it行業(yè),ajax那就是必須要掌握的一名技術(shù),下面我們就來對ajax進(jìn)行介紹,需要的朋友可以參考下2023-05-05

