學(xué)習(xí)本文之前請(qǐng)先看我的另一篇文章JAVA對(duì)XML節(jié)點(diǎn)的操作可以對(duì)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
|
package vastsum; import java.io.File; import java.io.FileWriter; import java.util.Iterator; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.junit.Test; /** * 使用dom4j操作xml * 對(duì)xml屬性操作 * 時(shí)間:2016年10月2號(hào) * 操作xml文件為contact.xml * 本文件文件名為:attrDemo.java * @author shutu008 * */ public class attrDemo{ @Test public void exmple() throws Exception{ //讀取XML文件,獲得document對(duì)象 SAXReader reader = new SAXReader(); Document document = reader.read( new File( "./src/contact.xml" )); //獲得某個(gè)節(jié)點(diǎn)的屬性對(duì)象 Element rootElem = document.getRootElement(); //獲取根節(jié)點(diǎn)屬性對(duì)象 Attribute rootAttr = rootElem.attribute( "id" ); //獲取指定節(jié)點(diǎn)屬性對(duì)象 Element contactElem = rootElem.element( "contact" ); Attribute contactAttr = contactElem.attribute( "id" ); //遍歷某個(gè)節(jié)點(diǎn)的所有屬性 for (Iterator it = contactElem.attributeIterator();it.hasNext();){ Attribute conAttr= (Attribute)it.next(); String conTxt = conAttr.getValue(); String conAttrName = conAttr.getName(); System.out.println(conAttrName+ " = " +conTxt); } //設(shè)置某節(jié)點(diǎn)的屬性和值 contactElem.addAttribute( "name" , "zhangsan" ); //設(shè)置(更改)某屬性的值 Attribute nameAttr = contactElem.attribute( "name" ); nameAttr.setValue( "lisi" ); //刪除某節(jié)點(diǎn)的指定屬性 contactElem.remove(nameAttr); //將某節(jié)點(diǎn)的屬性和值寫(xiě)入xml文檔中 XMLWriter writer = new XMLWriter( new FileWriter( "./src/contact.xml" )); writer.write(document); writer.close(); /** * 如果文檔中有中文需要設(shè)置字符編碼 * 用如下語(yǔ)句: * OutputFormat format = OutputFormat.createPrettyPrint(); * format.setEncoding("GBK"); * XMLWriter writer = new XMLWriter(new FileWriter("./src/contact.xml"),format); */ //獲取指定對(duì)象的屬性名 System.out.println(rootAttr.getName()); System.out.println(contactAttr.getName()); //獲取指定對(duì)象的屬性值 System.out.println(contactAttr.getValue()); System.out.println(rootAttr.getValue()); } } |
備注:以上例子代碼可以直接運(yùn)行。可以使用Junit 4調(diào)節(jié)本例子的代碼。
以下是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
|
<?xml version= "1.0" encoding= "UTF-8" ?> <contactList id= "0" > <contact id= "001" class = "style" > <name>張三</name> <age> 20 </age> <phone> 134222223333 </phone> <email>zhangsan @qq .com</email> <qq> 432221111 </qq> </contact> <contact id= "002" > <name>李四</name> <age> 20 </age> <phone> 134222225555 </phone> <email>lisi @qq .com</email> <qq> 432222222 </qq> </contact> <contactTwo> <name>王五</name> <age> 32 </age> <phone> 465431341 </phone> <emali>af @qq .com</emali> <qq> 46164694 </qq> </contactTwo> <test>測(cè)試</test> <test>其他用途</test> </contactList> |
文件目錄如圖所示:
以上就是小編為大家?guī)?lái)的java對(duì)xml節(jié)點(diǎn)屬性的增刪改查實(shí)現(xiàn)方法全部?jī)?nèi)容了,希望大家多多支持服務(wù)器之家~