程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 往xml中更新節點,xml更新節點

往xml中更新節點,xml更新節點

編輯:JAVA綜合教程

往xml中更新節點,xml更新節點


/* 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()) {
//循環添加至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);
//創建一個TransformerFactory對象
TransformerFactory tFactory = TransformerFactory.newInstance();
//得到一個操作對象
Transformer transformer = tFactory.newTransformer();
//設置們,這個是編碼
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);*/

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved