Java使用DOM4j实现读写XML文件的属性和元素

2025-05-29 0 40

dom4可以读取和添加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
public static void readattributes() throws documentexception {

file file = new file("d:\\\\cmz\\\\java\\\\xmltest\\\\customertest.xml");

saxreader reader = new saxreader();

document doc = reader.read(file);

element root = doc.getrootelement();

try {

for (iterator iterator = root.elementiterator(); iterator.hasnext();) {

element element = (element) iterator.next();

string customerid = element.attributevalue("customerid");

system.out.println("customerid = " + customerid);

string companyname = element.attributevalue("companyname");

system.out.println("companyname = " + companyname);

system.out.println("contactname = "

+ element.attributevalue("contactname"));

system.out.println("contacttitle = "

+ element.attributevalue("contacttitle"));

system.out.println("address = "

+ element.attributevalue("address"));

system.out.println("city = " + element.attributevalue("cit阿y"));

system.out.println("postalcode = "

+ element.attributevalue("postalcode"));

system.out.println("country = "

+ element.attributevalue("country"));

system.out

.println("phone = " + element.attributevalue("phone"));

system.out.println("fax = " + element.attributevalue("fax"));

system.out

.println("--------------------------------------------------------\\t");

}

} catch (exception e) {

// todo: handle exception

}

}

读取元素:

?

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
public static void readinnertest() throws documentexception {

file file = new file("d:\\\\cmz\\\\java\\\\xmltest\\\\customer1.xml");

saxreader reader = new saxreader();

document doc = reader.read(file);

element root = doc.getrootelement();

try {

for (iterator iterator = root.elementiterator(); iterator.hasnext();) {

element type = (element) iterator.next();

system.out.println(type.elementtext("customerid"));

system.out.println(type.elementtext("companyname"));

system.out.println(type.elementtext("contactname"));

system.out.println(type.elementtext("contacttitle"));

system.out.println(type.elementtext("address"));

system.out.println(type.elementtext("city"));

system.out.println(type.elementtext("postalcode"));

system.out.println(type.elementtext("country"));

system.out.println(type.elementtext("phone"));

system.out.println(type.elementtext("fax"));

system.out.println("---------------------------------\\t");

}

} catch (exception e) {

// todo: handle exception

}

}

写入属性:

?

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
public static void writeattributes() {

document doc = documenthelper.createdocument();

element ele = doc.addelement("table");

for (int i = 1; i < 5; i++) {

element customers = ele.addelement("customers");

customers.addattribute("customerid", "alfki" + i);

customers.addattribute("companyname", "alfreds futterkiste" + i);

customers.addattribute("contactname", "maria anders" + i);

customers.addattribute("contacttitle", "sales representative" + i);

customers.addattribute("address", "obere str. 57");

customers.addattribute("city", "beijin");

customers.addattribute("postalcode", "12209");

customers.addattribute("country", "germany");

customers.addattribute("phone", "030-0074321");

customers.addattribute("fax", "030-0076545");

try {

xmlwriter writer = new xmlwriter(new filewriter(new file(

"customertest.xml")));

writer.write(doc);

writer.close();

} catch (exception e) {

// todo: handle exception

}

}

}

写出元素:

?

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
public static void writeinnertest(){

document doc = documenthelper.createdocument();

element ele = doc.addelement("table");

for (int i = 1; i < 5; i++) {

element customers = ele.addelement("row");

element customerid = ele.addelement("customerid");

customerid.settext("alfki" + i);

element companyname = ele.addelement("companyname");

companyname.settext("alfreds futterkiste" + i);

element contactname = ele.addelement("contactname");

contactname.settext("maria anders" + i);

element contacttitle = ele.addelement("contacttitle");

contacttitle.settext("sales representative" + i);

element address = ele.addelement("address");

address.settext("obere str. 57");

element city = ele.addelement("city");

city.settext("beijin");

element postalcode = ele.addelement("postalcode");

postalcode.settext("12209");

element country = ele.addelement("country");

country.settext("germany");

element phone = ele.addelement("phone");

phone.settext("030-0074321");

element fax = ele.addelement("fax");

fax.settext("030-0076545");

}

try {

xmlwriter writer = new xmlwriter(new filewriter(new file(

"customertest2.xml")));

writer.write(doc);

writer.close();

} catch (exception e) {

// todo: handle exception

}

}

可以修改属性的文本内容:

?

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
public static void readupdateattribute() throws documentexception{

file file = new file("d:\\\\cmz\\\\java\\\\xmltest\\\\customertest.xml");

saxreader reader = new saxreader();

document doc = reader.read(file);

element root = doc.getrootelement();

try {

for (iterator iterator = root.elementiterator("customers"); iterator.hasnext();) {

element element = (element) iterator.next();

string name = "alfki1";

if (name.equals(element.attributevalue("customerid")) ) {

attribute attr = element.attribute("customerid");

attr.setvalue("234");

element contactname = element.addelement("23424");

contactname.settext("676767" );

}

}

xmlwriter writer = new xmlwriter(new fileoutputstream(file));

writer.write(doc);

readattributes();

} catch (exception e) {

// todo: handle exception

}

}

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

原文链接:https://www.cnblogs.com/chengmuzhe/p/10127895.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java使用DOM4j实现读写XML文件的属性和元素 https://www.kuaiidc.com/110547.html

相关文章

发表评论
暂无评论