Java操作XML工具类XmlUtil详解

时间:2022-10-26 16:37:10

本文实例为大家分享了java操作xml工具类的具体代码,供大家参考,具体内容如下

一、代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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

?
1
2
3
4
5
<?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>

三、最终效果图

Java操作XML工具类XmlUtil详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/u010823625/article/details/61920159