Home > Article > Backend Development > Detailed introduction to XML generation of Java class code
Recently docked with third-party companiesInterface, the other party calls our http interface through request The entire xml string passed 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 javaobject to 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.
1. Download xsd.exe
(bottom of the page)
2. Run the following command on the console
xsd xxx.xml [/outputdir:directory]
to get the corresponding xsd file
Of course, the xsd generated by the above method treats all
of xml as string, 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!