The JAXB tool for Java operation XML

Source: Internet
Author: User

In the work of manipulating XML in Java, the more convenient tool is JAXB (Java Architecture for XML Binding).

This tool makes it easy to generate XML tags and Java class correspondence. Refer to the information on the Internet for a brief explanation of some of the Java operations XML.

1. First define an XML schema file. Like what:

[HTML]View PlainCopyprint?
  1. <? XML version= "1.0" encoding="UTF-8" standalone="yes"?>
  2. <xs:schema version="1.0"
  3. xmlns:xs="Http://www.w3.org/2001/XMLSchema">
  4. <xs:element name="person">
  5. <xs:complextype>
  6. <xs:attribute name="name" type="xs:string"/>
  7. <xs:attribute name="age" type="Xs:int"/>
  8. </xs:complextype>
  9. </xs:element>
  10. </xs:schema>


The relevant XML schema definition rules can refer to http://www.w3.org/TR/xmlschema-2/

2. Generate the Java class.

Executing at the command line

C:\JAXB>XJC person.xsd

This generates the Persion class.

[Java]View PlainCopyprint?
  1. Package Com.test.xml
  2. Public class Person {
  3. protected String name;
  4. protected Integer age;
  5. Public String GetName () {
  6. return name;
  7. }
  8. public void SetName (String value) {
  9. this.name = value;
  10. }
  11. Public Integer Getage () {
  12. return age;
  13. }
  14. public void Setage (Integer value) {
  15. this.age = value;
  16. }
  17. }



The type of tag and the corresponding relationship of the Java type.

XML Schema Type Java Data Type
Xsd:string Java.lang.String
Xsd:integer Java.math.BigInteger
Xsd:int Int
Xsd.long Long
Xsd:short Short
Xsd:decimal Java.math.BigDecimal
Xsd:float Float
Xsd:double Double
Xsd:boolean Boolean
Xsd:byte Byte
Xsd:qname Javax.xml.namespace.QName
Xsd:datetime Javax.xml.datatype.XMLGregorianCalendar
Xsd:base64binary Byte[]
Xsd:hexbinary Byte[]
Xsd:unsignedint Long
Xsd:unsignedshort Int
Xsd:unsignedbyte Short
Xsd:time Javax.xml.datatype.XMLGregorianCalendar
Xsd:date Javax.xml.datatype.XMLGregorianCalendar
Xsd:g Javax.xml.datatype.XMLGregorianCalendar
Xsd:anysimpletype Java.lang.Object
Xsd:anysimpletype Java.lang.String
Xsd:duration Javax.xml.datatype.Duration
Xsd:notation Javax.xml.namespace.QName

3. Read the contents of the XML to the Java object.

Java reads An example of the contents of XML.

[Java]View PlainCopy print?
  1. Public class Unmarshallerutil {
  2. Public Unmarshallerutil () {
  3. try {
  4. //1. Generating Jaxbcontex Objects
  5. Jaxbcontext context = jaxbcontext.newinstance ("Com.test.xml");
  6. //2. Generating Unmarsaller objects
  7. Unmarshaller Unmarshaller = Context.createunmarshaller ();
  8. File File = new file ("Artists.xml");
  9. //3. Unmarsaller
  10. Object obj = unmarshaller.unmarshal (file);
  11. Persons artists = (Persons) obj;
  12. //Other processing
  13. } catch (Jaxbexception ex) {
  14. }
  15. }
  16. public static void Main (string[] args) {
  17. new Unmarshallerutil ();
  18. }
  19. }



The JAXB tool for Java operation XML

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.