How to use Jaxb2 in IJ to generate a file of the corresponding Java entity class using XML definitions

Source: Internet
Author: User

#0. To prepare the XML file to be converted, in Project vision, right-click the XML file, and on the popup menu, select "Generate xsd schema from XML files ..." and generate the XSD file by default.
Move the XSD file to the path specified in the # Config section of Configuration/sources/source.

#1. Open Pom.xml and add the following configuration section. The contents of the configuration node are set differently depending on the project. Generally, there are 3 settings: packagename,outputdirectory and sources,
For specific official documentation, see: http://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v2.2/xjc-mojo.html

<Build>        <pluginmanagement>            <Plugins>                <plugin>                    <groupId>Org.apache.maven.plugins</groupId>                    <Artifactid>Maven-compiler-plugin</Artifactid>                    <Configuration>                        <Source>1.8</Source>                        <Target>1.8</Target>                    </Configuration>                </plugin>            </Plugins>        </pluginmanagement>        <Plugins>            <plugin>                <groupId>Org.codehaus.mojo</groupId>                <Artifactid>Jaxb2-maven-plugin</Artifactid>                <version>2.3.1</version>                <executions>                    <Execution>                        <ID>Xjc</ID>                        <Goals>                            <goal>Xjc</goal>                        </Goals>                    </Execution>                </executions>                <Configuration>                    <!--The package of your generated sources, JAXB will generate directories at this package level and generate Java classes into this package -                    <PackageName>Jaxbhelper.entity</PackageName>                                        <!--The path of your generated sources class, output the root directory address of the Java class -                    <outputdirectory>D:\Git\zfq308\JaxbHelper\src\main\java\</outputdirectory>                    <sources>                        <!--This is the path to the XSD, which can be supported by setting multiple XSD files under the sources node -                        <Source>D:\Git\zfq308\JaxbHelper\src\main\resources\FeedTestCase1.xsd</Source>                    </sources>                </Configuration>            </plugin>                   </Plugins>    </Build>
View Code

#2. After adding the XML node shown in # # in the Pom file, in the MAVEN Projects Vision window, locate the node for your project, under the Plugins node,
Locate the Jaxb2 node, and after you expand, double-click the Execute JAXB2:XJC task to generate the corresponding entity under the specified output folder.


#3. When you load XML data into a concrete class object instance through JAXB, you can use the following code: In this case, Testsuitestype is the root node of the XML.

ImportJaxbHelper.Entity.TestSuitesType;ImportJavax.xml.bind.JAXBContext;Importjavax.xml.bind.JAXBException;ImportJavax.xml.bind.Unmarshaller;ImportJava.io.File; Public classApp { Public Static voidMain (string[] args)throwsjaxbexception {File file=NewFile ("D:\\git\\zfq308\\jaxbhelper\\src\\main\\resources\\feedtestcase1.xml"); Jaxbcontext Jaxbcontext= Jaxbcontext.newinstance (Testsuitestype.class); Unmarshaller Jaxbunmarshaller=Jaxbcontext.createunmarshaller (); Testsuitestype testsuites=(testsuitestype) jaxbunmarshaller.unmarshal (file); Integer I= 0;//This line is used for breakpoint testing            }}

Note: When executed directly, the compiler compiles the pass, but runs the Times: Exception in thread "main" javax.xml.bind.UnmarshalException:unexpected Element (URI: "", Local: "Testsuites"). Expected elements is (none) error.
The reason for this is that JAXB does not know which Java classes you generate, and who is the primary node. To do this, add a callout to the class that corresponds to the XML root node: @XmlRootElement (name= "Testsuites")

Prior to joining:

= "Testsuitestype", Proporder = {"TestSuite"})publicclass  testsuitestype {}

After joining:

@XmlRootElement (name= "testsuites")= "Testsuitestype", Proporder = {"TestSuite"})  publicclass  testsuitestype {}

Can be used normally after modification.


#4. If the XML has to be modified to regenerate XSD, Java classes, and so on, you can delete the corresponding XSD, Java class and Meta-inf folder, delete the target directory if necessary , and then repeat the #2-#3即可.

How to use Jaxb2 in IJ to generate a file of the corresponding Java entity class using XML definitions

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.