您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“如何往xml中更新節(jié)點”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“如何往xml中更新節(jié)點”這篇文章吧。
往xml中更新節(jié)點的實例代碼
/* 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(); //設置們,這個是編碼 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é)點”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。