一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java操作XML工具類XmlUtil詳解

Java操作XML工具類XmlUtil詳解

2021-06-23 14:26u010823625 Java教程

這篇文章主要為大家詳細介紹了Java操作XML工具類XmlUtil的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 大伊香蕉精品二区视频在线 | 91精品国产亚洲爽啪在线影院 | 荡娃艳妇系列小说 | 国产精品边做边接电话在线观看 | 超h 超重口 高h 污肉1v1 | 国产一区二区精品久 | 果冻传媒天美传媒网址入口 | 青青草原在线免费 | 五月色婷婷网在线观看 | 美女大乳被捏羞羞漫画 | 亚洲 欧美 国产 在线观看 | 国产综合亚洲欧美日韩一区二区 | 免费观看大片毛片 | 国产伦精一区二区三区视频 | 国产精品igao视频网网址 | 欧美一区二区三区免费观看视频 | 高清毛片aaaaaaaaa片 | 精品日本三级在线观看视频 | 久久人妻熟女中文字幕AV蜜芽 | 99精品国产高清一区二区三区香蕉 | 精品欧美一区二区三区在线观看 | 奇米影视888四色首页 | 欧美怡红院视频一区二区三区 | 特黄aa级毛片免费视频播放 | 2022国产麻豆剧果冻传媒入口 | 我的男友是消防员在线观看 | 欧美添下面视频免费观看 | 出差上的少妇20p | 国产精品国产色综合色 | 国产白虎 | 国产麻豆91网在线看 | 果冻传媒九一制片厂 | 美女逼逼喷水 | 秀婷程仪公欲息肉婷在线观看 | 国产欧美日韩精品在线 | 国内自拍网红在线综合 | 日日插插| 99热精品成人免费观看 | 日本一区二区视频免费播放 | 爽好舒服使劲添高h视频 | 国产成人亚洲综合网站不卡 |