Jaxb: conversion between Java Bean and XML

Source: Internet
Author: User

1. What is jaxb?
Java architecture for XML binding (jaxb) is an industry standard and a technology that can generate Java classes based on XML schema. In this process, jaxb also provides a way to reverse generate a Java object tree for the XML instance document, and re-write the content of the Java object tree to the XML instance document. On the other hand, jaxb provides a fast and easy way to bind the XML schema to the Java representation, so that Java developers can easily combine XML data and processing functions in Java applications.
This means that you can utilize the flexibility of the platform's core XML data in Java applications without processing or even knowing XML programming skills. Furthermore, you can take full advantage of XML without relying on complex XML processing models such as sax or Dom. Jaxb hides the details and removes the useless relationship between Sax and Dom. The generated jaxb class only describes the relationships defined in the original model. The result is a combination of Highly portable Java code and highly portable XML data. These codes can be used to create flexible and lightweight applications and Web services.

2. jaxb Architecture

The architecture and application process of jaxb generally includes the following steps:

1) write an XML Schema Based on the XML data format your application wants to operate on. For more information about XML schema, see "references"

2) use the jaxb compilation tool (binding compiler) to input the XML schema file and generate a series of related Java
Class and Interface

3) when using the jaxb compilation tool, you can selectively provide a configuration file (dotted line of the graph) to control some advanced attributes of the jaxb compilation tool.

4) These Java classes and interfaces are the main interfaces and methods for your application to manipulate XML data.

5) operations on XML documents through jaxb mainly include parsing XML documents that comply with XML schema to generate a set of corresponding Java objects; perform operations on these objects (modify, add, and delete object attributes), and save the content of these objects to this XML document.

 

 

 

package com.test;import javax.xml.bind.annotation.XmlAttribute;  import javax.xml.bind.annotation.XmlElement;    public class User {      private int id;      private String name;         @XmlAttribute (name = "id")      public int getId() {          return id;      }         public void setId(int id) {          this.id = id;      }         @XmlElement(name = "name")      public String getName() {          return name;      }         public void setName(String name) {          this.name = name;      }         public String toString() {          return id + ":" + name;      }  }  

 

package com.test;  import java.util.ArrayList;import java.util.List;    import javax.xml.bind.annotation.XmlElement;  import javax.xml.bind.annotation.XmlRootElement;    @XmlRootElement(name = "users")  public class UserList {    List<User> users = new ArrayList<User>();         @XmlElement(name = "user")      public List<User> getUsers() {          return users;      }         public void setUsers(List<User> users) {          this.users = users;      }  }  

 

package com.test;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import java.util.ArrayList;import javax.xml.bind.JAXBContext;import javax.xml.bind.Marshaller;public class UserGenerateClient {public static void main(String[] args) {try {JAXBContext jc = JAXBContext.newInstance(UserList.class);Marshaller m = jc.createMarshaller();OutputStream os = new FileOutputStream(new File("user2.xml"));UserList list = new UserList();User user = new User();user.setId(100);user.setName("Shawn");list.getUsers().add(user);user = new User();user.setId(200);user.setName("Jason");list.getUsers().add(user);m.marshal(list, os);} catch (Exception e) {e.printStackTrace();}}}

 

package com.test;import java.net.URL;import javax.xml.bind.JAXBContext;import javax.xml.bind.Unmarshaller;public class UserReadClient {public static void main(String[] args) throws Exception {JAXBContext context = JAXBContext.newInstance(UserList.class);Unmarshaller unmarshaller = context.createUnmarshaller();  URL xmlFileUrl = UserReadClient.class.getResource("user2.xml");UserList userList = (UserList) unmarshaller.unmarshal(xmlFileUrl);for (User m: userList.getUsers()){System.out.println(m.getName());System.out.println(m.getId());}}}

 

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.