[Java development series] - JDOM to create, modify, delete, read XML file

There are many ways of manipulating XML documents, tell us about the methods and techniques used here for JDOM.

JDOM Download

Create an XML document

  XML file is a typical tree-file, each document is a document element children of the element. And each sub-element is an Element object, the object may comprise downwardly.

  1 so we can add to the parent element by creating an element elements first and then finally add the top-level element to the root element.

  2 After you create a document element, you can add elements to the document object, and then written to the file.

  The main use of functions:

Element.setAttribute 为元素添加信息

Element.addContent(String,String) 为元素添加子元素内容,也可以直接添加另一个元素节点

Document.setRootElement(Element) 为文档添加根元素

XMLOutputter.output(Document,FileWriter) 将Docuemnt写入到FileWriter文件流中

 

  The following are the main operation, the process of writing to files placed in saveXML

 1     @SuppressWarnings("null")
 2     public static void createXML() {
 3         // 创建document
 4         Document mydoc = new Document();
 5 
 6         // 创建元素person1
 7         Element person1 = new Element("person");
 8         person1.setAttribute("id", "ID001");
 9         // 添加注释
10         person1.addContent(new Comment("this is person1"));
11 
12         person1.addContent(new Element("name").setText("xingoo"));
13         person1.addContent(new Element("age").setText("25"));
14         person1.addContent(new Element("sex").setText("M"));
15         // 可以嵌套添加子元素
16         Element address1 = new Element("address");
17         address1.setAttribute("zone", "province");
18         address1.addContent("LiaoNing");
19         person1.addContent(address1);
20 
21         // 创建元素person2
22         Element person2 = new Element("person");
23         person2.setAttribute("id", "ID002");
24         // 添加注释
25         person2.addContent(new Comment("this is person2"));
26 
27         person2.addContent(new Element("name").setText("xhalo"));
28         person2.addContent(new Element("age").setText("26"));
29         person2.addContent(new Element("sex").setText("M"));
30         // 可以嵌套添加子元素
31         Element address2 = new Element("address");
32         address2.setAttribute("zone", "province");
33         address2.addContent("JiLin");
34         person2.addContent(address2);
35 
36         // 在doc中添加元素Person
37         Element info = new Element("information");
38         info.addContent(person1);
39         info.addContent(person2);
40         mydoc.setRootElement(info);
41         
42         saveXML(mydoc);
43     }

the saveXML () Code:

 1     public static void saveXML(Document doc) {
 2         // 将doc对象输出到文件
 3         try {
 4             // 创建xml文件输出流
 5             XMLOutputter xmlopt = new XMLOutputter();
 6 
 7             // 创建文件输出流
 8             FileWriter writer = new FileWriter("person.xml");
 9 
10             // 指定文档格式
11             Format fm = Format.getPrettyFormat();
12             // fm.setEncoding("GB2312");
13             xmlopt.setFormat(fm);
14 
15             // 将doc写入到指定的文件中
16             xmlopt.output(doc, writer);
17             writer.close();
18         } catch (Exception e) {
19             e.printStackTrace();
20         }
21     }

  After the execution, it refreshes the project, you can see person.xml files in the project.

Read XML documents

  Read the document, you first need an xml parser, it can automatically parse out the individual elements, and the sub-elements as its child nodes, easy to operate.

  The main use of functions:

SAXBuilder.build("xxx.xml") 解析XML文档

Document.getRootElement() 获取根元素

Element.getChildren() 获取根元素下的子元素,返回List<Element>

Element.getAttributeValue(String) 获取指定元素的信息

Element.getChildText 获取指定元素的内容

 

 1     public static void readXML() {
 2         // 使用SAXBuilder解析器解析xml文件
 3         SAXBuilder sb = new SAXBuilder();
 4         Document doc = null;
 5         try {
 6             doc = sb.build("person.xml");
 7             Element root = doc.getRootElement();
 8             List<Element> list = root.getChildren("person");
 9             for (Element el : list) {
10                 String id = el.getAttributeValue("id");
11                 String name = el.getChildText("name");
12                 String age = el.getChildText("age");
13                 String sex = el.getChildText("sex");
14                 System.out.println("id:" + id);
15                 System.out.println("name:" + name);
16                 System.out.println("age:" + age);
17                 System.out.println("sex:" + sex);
18                 System.out.println("--------------------------");
19             }
20         } catch (Exception e) {
21             e.printStackTrace();
22         }
23     }

 

Modify XML documents

  Modify XML documents, but also the first use of the parser finds the specified element using setText or setAttributeValue to modify the element content

  Remember to save the modified file, that is, in this call saveXML ()

 1     public static void updateXML(){
 2         SAXBuilder sb = new SAXBuilder();
 3         Document doc = null;
 4         try {
 5             doc = sb.build("person.xml");
 6             Element root = doc.getRootElement();
 7             List<Element> list = root.getChildren("person");
 8             for (Element el : list) {
 9                 if (el.getAttributeValue("id").equals("ID001")) {
10                     Element name = el.getChild("name");
11                     name.setText("xingoo---update");
12                 }
13             }
14         } catch (Exception e) {
15             e.printStackTrace();
16         }
17         saveXML(doc);
18     }

Deleting an XML document element

 

  Delete similar modifications, call removeCotent you can delete the specified content elements according to the. But to use his parent calls. Finally, also need to be saved to a file can.

 1     public static void removeXML() {
 2         SAXBuilder sb = new SAXBuilder();
 3         Document doc = null;
 4         try {
 5             doc = sb.build("person.xml");
 6             Element root = doc.getRootElement();
 7             List<Element> list = root.getChildren("person");
 8             for (Element el : list) {
 9                 if (el.getAttributeValue("id").equals("ID001")) {
10                     root.removeContent(el);
11                 }
12             }
13         } catch (Exception e) {
14             e.printStackTrace();
15         }
16         saveXML(doc);
17     }

Reproduced in: https: //my.oschina.net/u/204616/blog/545026

Guess you like

Origin blog.csdn.net/weixin_33748818/article/details/91989510