ACCP8.0 Extensions - Parsing xml files using JAXB (1)

We have learned the basics of xml and how to read it in C# in the second semester of C# (in-depth.NET platform and C# programming), but we haven't talked about how to manipulate and read xml in Java. Here I'll give you a brief introduction to several ways of parsing in Java for understanding.

 

1. Several ways to parse xml in Java

1.1 JDK Native dom Form Principle: Read xml into memory at once and build a tree structure in memory.Advantages: Easy to operate on nodes, Disadvantages: Requires a lot of memory space, wastes resources

1.2 SAX Form Principle: Based on event form, when the parser finds an element start, end, text, start or end of a document, send events, and the programmer writes code that responds to these events to save data.Advantages: Faster and less resource consuming than the dom format.Disadvantage: No persistence.After the event, if the data is not saved, it is lost.

1.3 DOM4J is an open source framework with excellent performance, powerful functionality and extreme ease of use.

1.4 JAXB (Java Architecture for XML Binding) is an industry standard and a technology for generating Java classes from XML Schema.

 

2. Several core objects of JAXB

2.1 JAXBContext Jaxb context, through which we get two other core objects Unmarshaller (for parsing xml) and Marshaller (for generating xml)

2.2 JAXBContext usually uses its static method newInstance(Class className) to get objects

2.3 Unmarshaller is used to parse xml obtained by the createUnmarshaller method of JAXBContext

2.4 Marshaller is used to generate xml obtained by the createMarshaller method of JAXBContext

 

3. Sample Code

3.1 Entity Classes Bean and Property

 1 package org.lyrk.accp8.s2.chapter.xml;
 2 
 3 import javax.xml.bind.annotation.*;
 4 
 5 /**
 6  * Created by niechen on 17/5/9.
 7  */
 8 @XmlRootElement
 9 @XmlAccessorType(XmlAccessType.FIELD)
10 public class Bean {
11 
12     @XmlElement
13     private Property property;
14 
15     public Property getProperty() {
16         return property;
17     }
18 
19     public void setProperty(Property property) {
20         this.property = property;
21     }
22 }
23 
24 @XmlAccessorType(value = XmlAccessType.FIELD)
25 class Property {
26     @XmlAttribute(name = "id")
27     private String id;
28 
29     @XmlAttribute(name = "value")
30     private String value;
31 
32     public String getId() {
33         return id;
34     }
35 
36     public void setId(String id) {
37         this.id = id;
38     }
39 
40     public String getValue() {
41         return value;
42     }
43 
44     public void setValue(String value) {
45         this.value = value;
46     }
47 }

3.2 Main Function Class

 1 package org.lyrk.accp8.s2.chapter.xml;
 2 
 3 import javax.xml.bind.JAXBContext;
 4 import javax.xml.bind.JAXBException;
 5 import javax.xml.bind.Unmarshaller;
 6 import java.io.InputStream;
 7 
 8 /**
 9  * Created by niechen on 17/5/9.
10  */
11 public class Test {
12 
13     public static void main(String[] args) {
14         try {
15             JAXBContext jaxbContext = JAXBContext.newInstance(Bean.class);//Establish JAXBContext Object, be careful to pass@XmlRootElement The type of class marked by
16             Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();//Get it xml Resolve Object
17             InputStream inputStream = Bean.class.getClassLoader().getResourceAsStream("bean.xml");//stay classpath Lower Read xml File Stream
18             Bean bean = (Bean) unmarshaller.unmarshal(inputStream);//take xml Convert to Entity Object
19             System.out.println(bean.getProperty().getId());//output ID attribute
20             System.out.println(bean.getProperty().getValue());//output value attribute
21         } catch (JAXBException e) {
22             e.printStackTrace();
23         }
24     }
25 }

3.3 xml file content

1 <?xml version="1.0" encoding="UTF-8"?>
2 <bean>
3     <property id="name" value="10" />
4 </bean>

Keywords: Java xml Attribute Programming

Added by jeff_valken on Tue, 02 Jul 2019 20:02:14 +0300