Detailed introduction to XML generation of Java class code

黄舟
Release: 2017-03-30 13:59:16
Original
2222 people have browsed it

Recently docked with third-party companiesInterface, the other party calls our http interface through request The entirexmlstringpassed in the body, the definition of xml is defined by a third-party company. After receiving the string, I need to parse the xml content and parse out the business I want. Data. Previously, JAXB was used to convert between xml and beans. First define the java bean, then add the xml annotation, and then use JAXB to convert the javaobjectto xml, or convert the xml to java object.

The current problem is that there is no such java bean class, and a java bean needs to be defined first. However, there is too much xml content, and it is not an xml. There are many types. It is too laborious to type one by one. I checked online. Solve the problem.

JAXB generates java bean code based on xml xsd file

1. Download the JAXB package

jaxb-2_1_9.zip(Bottom of the page)

2. Unzip the command line and 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

JAXB generates java code The input parameter is an xsd file, not an xml file, so the xsd file must be generated first. .

The XML Schema Definition (Xsd.exe) tool generates XML schema or common lang uage run time class es from XDR, XML, and XSD file s, or from classes in a runtime assembly.
Copy after login

1. Download xsd.exe

(bottom of the page)

2. Run the following command on the console

xsd xxx.xml [/outputdir:directory]
Copy after login

to get the corresponding xsd file


Of course, the xsd generated by the above method treats all

attributes

of xml asstring, so all the attributes of the generated java bean are also It's a string, you need to modify it appropriately, but it's much easier than typing one by one, and it's not easy to make mistakes.

The above is the detailed content of Detailed introduction to XML generation of Java class code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!