Describes how to generate Java code in XML

Source: Internet
Author: User
Recently, it was connected to a third-party company interface. when the other party calls our http interface, the entire xml string is passed in the request body. xml is defined by a third-party company. After receiving the string, I want to parse the xml content and parse the business data I want. Previously, JAXB was used for the conversion between xml and bean. javabean was first defined, followed by xml annotations. then, JAXB was used to convert java objects into xml objects or convert xml into java objects. Recently connected to a third-party company # code/11829.html "target =" _ blank "> interface, the other party calls our http interface to upload the entire xml string in the request body, xml is defined by a third-party company. After receiving the string, I want to parse the xml content and parse the business data I want. Previously, JAXB was used for the conversion between xml and bean. java beans were defined first, followed by xml annotations, and then java objects were converted to xml using JAXB or xml into java objects.

The problem is that this java bean class does not exist. you need to define a java bean first. However, there is too much xml content, and it is not an xml file. There are many types of xml files. one by one is too laborious. I checked them online to solve the problem.

JAXB generates java bean code based on the xml xsd file

1. download the JAXB package

Jaxb-2_1_9.zip (at the bottom of the page)

2. unzip the command line to enter the bin directory.

3. run: xjc-d % output_path %-p com. xxx. xxx. bean xxx. xsd

-D: java code storage path

-P: bean package structure

Use xsd.exe to generate the xsd file corresponding to the xml file

The input parameter of java code generated by JAXB is an xsd file, not an xml file.

Xsd.exe is a tool of Microsoft. net. it is borrowed, but it will serve my java later.

The XML Schema Definition (Xsd.exe) tool generates XML schema or common language runtime classes from XDR, XML, and XSD files, or from classes in a runtime assembly.

1.download xsd.exe

(Bottom of the page)

2. run the following command on the console:

   xsd xxx.xml [/outputdir:directory]

In this way, the corresponding xsd file is generated.

Of course, all xml attributes are treated as strings in the xsd generated by the above method. Therefore, all attributes of the generated java bean are also strings and must be modified as needed, however, it is much easier than a single knock, and it is not prone to errors.

The preceding section details how to generate Java code in XML. For more information, see other related articles in the first PHP community!

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.