往xml中更新节点的实例代码
程序员文章站
2023-02-21 20:28:23
往xml中更新节点的实例代码
/* system.out.println("2323");
documentbuilderfactory factory = d...
往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);*/
以上这篇往xml中更新节点的实例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。