服务器之家

服务器之家 > 正文

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

时间:2021-06-22 13:29     来源/作者:程沐喆

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
  }
  
 }

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

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

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部