JAXB Parsing xml

Source: Internet
Author: User

In the traditional way, we are all through the dom4j or Jdom way to parse the XML, the general is to parse the entire XML into memory document, and then hierarchical traversal of the document tree, so there are several bad places, the first is the memory, there is the code is inflexible, Cannot parse for common XML, but JAXB is not the same, it can be parsed for any type of XML, even if the XML changes, you can use only that small amount of code, without changing the code logic, as follows:

1, generating XSD

First, we generate the XSD file corresponding to the existing XML file, The method is Xsd.exe, this EXE file is provided by Microsoft, we can find this file in our local, and then put the XML file in the same directory as the EXE file, for example, our XML file name is Resource.xml, then we go to the command line, and then to the EXE file in the path, through the XSD Resource.xml, you can generate the corresponding resource.xsd, which is the XSD file we need.

2, generate the corresponding code defined in the XSD

The JDK provides a xjc.jar for us to convert the XSD into corresponding code, as follows:

Xjc–d d:\–p com.huawei.test resource.xsd

The above parameters are defined as follows:-D refers to the path where the XSD file resides, and-P refers to the package name where the generated code resides.

3. Read the XML file into an in-memory object

If a customer is defined in our resource.xml, then we can read the XML into our in-memory customer object in the following way:

(1) Marshal, generate the XML Memory object:

 Public void Marshaltoobject () {   new File ("c:\\file1.xml");     Jaxbcontext Jaxbcontext = jaxbcontext.newinstance (Customer.  Class);     Marshaller Jaxbmarshaller = Jaxbcontext.createmarshaller ();     //Output pretty printed     true);     Jaxbmarshaller.marshal (customer, file);     Jaxbmarshaller.marshal (Customer, System.out);  }

(2) Unmarshaller, generate an XML file for the objects created in memory:

 Public void Unmarshallertoxml () {    new File ("c:\\file.xml");      Jaxbcontext Jaxbcontext = jaxbcontext.newinstance (Customer.  Class);      Unmarshaller Jaxbunmarshaller = Jaxbcontext.createunmarshaller ();      Customer customer = (customer) jaxbunmarshaller.unmarshal (file);      SYSTEM.OUT.PRINTLN (customer);  }

JAXB Parsing 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.