Java操作XML工具类XmlUtil详解
程序员文章站
2024-03-03 21:01:22
本文实例为大家分享了java操作xml工具类的具体代码,供大家参考,具体内容如下
一、代码
public class xmlutil {
/**
* 将...
本文实例为大家分享了java操作xml工具类的具体代码,供大家参考,具体内容如下
一、代码
public class xmlutil { /** * 将xml文件输出到指定的路径 * * @param doc * @param filename * @throws exception */ public static void outputxml(document doc, string filename) throws exception { transformerfactory tf = transformerfactory.newinstance(); transformer transformer = tf.newtransformer(); domsource source = new domsource(doc); transformer.setoutputproperty(outputkeys.encoding, "utf-8"); transformer.setoutputproperty(outputkeys.indent, "yes"); printwriter pw = new printwriter(new fileoutputstream(filename)); streamresult result = new streamresult(pw); transformer.transform(source, result); system.out.println("生成xml文件成功!"); } /** * 生成xml * * @param ip * @return */ public static document generatexml(string ip) { document doc = null; element root = null; try { documentbuilderfactory factory = documentbuilderfactory .newinstance(); documentbuilder builder = factory.newdocumentbuilder(); doc = builder.newdocument(); root = doc.createelement("errordevices"); doc.appendchild(root); } catch (exception e) { e.printstacktrace(); return null;// 如果出现异常,则不再往下执行 } element element; element = doc.createelement("errordevice"); element.setattribute("ip", ip); element.setattribute("date", stringutil.formatdate(new date(), "yyyy-mm-dd hh:mm:ss")); element.setattribute("status", "1"); root.appendchild(element); return doc; } /** * 新增xml节点 * * @param ip * @param filename * @return * @throws filenotfoundexception * @throws transformerexception */ public static void towrite(string filename, string ip) throws filenotfoundexception, transformerexception { string date = stringutil.formatdate(new date(), "yyyy-mm-dd hh:mm:ss"); documentbuilderfactory factory = documentbuilderfactory.newinstance(); documentbuilder builder = null; document doc = null; try { builder = factory.newdocumentbuilder(); doc = builder.parse(new file(filename)); } catch (parserconfigurationexception e) { e.printstacktrace(); } catch (saxexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } nodelist links = doc.getelementsbytagname("errordevice"); if (links.getlength() > 0) { for (int i = 0; i < links.getlength(); i++) { node nd = links.item(i); node catparent = nd.getparentnode(); element ele = (element) nd; string url = ele.getattribute("ip"); if (url.equals(ip)) { // ele.setattribute("date", date); catparent.removechild(nd); } } } element element = doc.createelement("errordevice"); element.setattribute("ip", ip); element.setattribute("date", stringutil.formatdate(new date(), "yyyy-mm-dd hh:mm:ss")); element.setattribute("status", "1"); doc.getdocumentelement().appendchild(element); transformerfactory tf = transformerfactory.newinstance(); transformer transformer = tf.newtransformer(); domsource source = new domsource(doc); transformer.setoutputproperty(outputkeys.encoding, "utf-8"); transformer.setoutputproperty(outputkeys.indent, "yes"); printwriter pw = new printwriter(new fileoutputstream(filename)); streamresult result = new streamresult(pw); transformer.transform(source, result); system.out.println("新增xml节点成功!"); } /** * 读取xml * * @param filename * @return */ public static list<map> readxml(string filename){ documentbuilderfactory factory = documentbuilderfactory.newinstance(); documentbuilder builder = null; document doc = null; try { builder = factory.newdocumentbuilder(); doc = builder.parse(new file(filename)); } catch (parserconfigurationexception e) { e.printstacktrace(); } catch (saxexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } nodelist links = doc.getelementsbytagname("errordevice"); list<map> list = new arraylist<map>(); for(int i = 0; i< links.getlength() ; i ++){ element node = (element)links.item(i); map map = new hashmap(); map.put(node.getattribute("ip"), node.getattribute("date")); list.add(map); } return list; } }
二、演示xml
<?xml version="1.0" encoding="utf-8" standalone="no"?> <errordevices> <errordevice date="2017-03-13 12:54:16" ip="20.100.156.42" status="1"/> <errordevice date="2017-03-13 12:54:56" ip="20.100.156.41" status="1"/> </errordevices>
三、最终效果图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。