ACCP8.0 extended content --- use JAXB to parse xml files (1), accp8.0jaxb

Source: Internet
Author: User

ACCP8.0 extended content --- use JAXB to parse xml files (1), accp8.0jaxb

Our second semester C # (in-depth. NET platform and C # programming) I have learned the basic knowledge of xml and the method for C # To Read xml, but I have not discussed the method for operating and reading xml in Java, here I will give you a brief introduction to several methods of parsing in java for your understanding.

 

1. Several Methods for parsing xml in java

1.1 JDK native dom format principle: one-time read xml into the memory, build a tree structure in the memory. Advantages: Easy to operate on nodes. Disadvantages: large memory space is required and resources are wasted.

1.2 principle of the SAX format: Based on the event format, when the parser finds the start or end of an element, the end of an element, the beginning or end of a text or document, it sends an event, and the programmer writes the code to respond to the event, save data. Advantage: It is faster than dom and consumes less resources. Disadvantage: No persistent words. After the event, if the data is not saved, the data will be lost.

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

1.4 JAXB (Java Architecture for XML Binding) is an industry standard and a technology that can generate Java classes based on XML Schema.

 

2. Several core JAXB objects

2.1 JAXBContext the context of Jaxb. Through this object, we can obtain the other two core objects Unmarshaller (used to parse xml) and Marshaller (generate xml)

2.2 JAXBContext usually uses its static method newInstance (Class className) to obtain the object

2.3 Unmarshaller is used to parse xml and obtained through the createUnmarshaller method of JAXBContext.

2.4 Marshaller is used to generate xml and obtained through the createMarshaller method of JAXBContext.

 

3. Sample Code

3.1 entity-class 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     @XmlElement13     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); // create a JAXBContext object. Be sure to pass the type of the class marked by @ XmlRootElement 16 Unmarshaller unmarshaller = jaxbContext. createUnmarshaller (); // get the xml parsing object 17 InputStream inputStream = Bean. class. getClassLoader (). getResourceAsStream ("bean. xml "); // read the xml file stream under classpath 18 Bean bean = (Bean) unmarshaller. unmarshal (inputStream); // converts xml into an 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>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.