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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 青青草99热这里都是精品 | 人与蛇boxxⅹ| 被老头肉至怀孕小说 | 乳 好大h | 日韩成人精品在线 | 天天做天天爽天天谢 | 色综合天天综合中文网 | free性俄罗斯护士 | 日本性爱 | 国产精品aaa| 午夜小视频免费 | 日韩免费一级 | 全色黄大色黄大片爽一次 | 17个农民工婉莹第一部 | 91国语精品自产拍在线观看一 | 亚洲毛片基地 | 亚洲精品久久久久AV无码 | 九九大香尹人视频免费 | 国产视频一区 | 91探花在线观看 | 欧美精品99久久久久久人 | 蜜桃麻豆 | 国产美女久久精品香蕉69 | www.色呦呦.com| 女子监狱第二季在线观看免费完整版 | 色综合欧美色综合七久久 | 草莓视频幸福宝 | 我与白丝同桌的故事h文 | 婷婷色网| 成人不卡在线 | 日韩专区 | 久久久久久免费高清电影 | 欧美1区 | 美女扒开腿让男生桶爽漫画 | 精品久久免费观看 | 日本色吧 | 女人爽到喷水的视频免费看 | 黄a 大片a v 永久免费 | 日本人添下面的全过程 | 视频一区久久 | 欧美一级高清免费a |