XML to java object structure

时间:2016-07-11 20:51:56

标签: java xml

I have XML file and want make java classes.

I want something like this

@XmlRootElement
public class Customer {

    String name;
    int age;
    int id;

    public String getName() {
        return name;
    }
}

But from bigger XML, some elements got duplicate child elements and some not. Create it manually is very complicated, so I tried some online tools like Pojo, but it makes class for every element (root element got 10 same child elements) so it makes 100+ class files, thats not the right solution in my opinion. I looked to JAXB, but I didnt find anything that is just creating class files, found only how to fill these classes. Tried convert XML to XSD and then to java classes, but didnt find anything what make java classes from XSD.

Thanks for every response, I tried many ways, but none was right.

3 个答案:

答案 0 :(得分:0)

Well there are several threads about this: How to generate JAXB classes from XSD? this one is nice if you use eclipse. If you´re using maven then check this site http://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v2.2/example_xjc_basic.html.

To check for more examples about the jaxb conversion lookup xjc java examples.

Hope it helps

答案 1 :(得分:0)

I found this on jetbrains. It talks about generating Java classes using XSD and JAXB.

https://www.jetbrains.com/help/idea/2016.1/generating-java-code-from-xml-schema.html

Here's an excerpt from that link:

To generate a Java class from an XML Schema using JAXB

  1. In the active editor tab, open the desired Schema (.xsd) file or an XML document which contains the desired Schema. Then choose Tools | JAXB | Generate Java Code From XML Schema Using JAXB on the main menu.

  2. In the Generate Java from Xml Schema using JAXB dialog box that opens configure the generation procedure:

    -In the Schema/DTD/WSDL Path drop-down list, specify the file to be used as the basis for code generation. By default, the field shows the full path to the current file. Accept this suggestion or click the Browse button [...] and select the desired file in the Select XML Schema File for JAXB Generation that opens.

    -From the Output Path drop-down list, select the module source directory to place the generated Java class in.

    -In the Package Prefix drop-down list, specify the package to include the generated stubs in.

    -Using the check boxes, configure additional options, such as generating annotation, setting the read-only status, downloading and installing additional libraries.

Hope this helps.

答案 2 :(得分:0)

  1. JAXB - This is default JDK parser based on DOM
  2. Castor- Third party tool using DOM
  3. XStream- Thirdparty tool uses SAX parser but no or limited namespace support

JAXB is by far the most easy to use and up to date tool in Java. It has options to handle most of the points you mentioned like duplicate elements etc. For duplicate elements if you generate fully qualified name spaces, then it should work. Assume XSD namespace is equivalent to Java package.

Refer this examples : Java to XML: JAXB with namespace unmarshalling (using Jersey from REST service)

XML to Java: JAXB :Need Namespace Prefix to all the elements