0

I want to create model classes from XML that I have written for my Java application. What are some good tools to convert auto generated classes from the XML I am writing ? A sample from my XML file.

  <list name="NameValuePairListDefinition">
    <member target="NameValuePair" />
  </list>

  <structure name="PositiveIntegerList">
    <member name="list" target="PositiveIntegerListDefinition" />
  </structure>
Vineet
  • 365
  • 1
  • 6
  • 20

1 Answers1

2

If you're working with Java and want to generate classes I would highly suggest going with XMLbeans. It comes with some nice utilities, including:

  • inst2xsd : given the xml , it generates xsd

    EXAMPLE

    inst2xsd -enumerations never message.xml

    (will generate xsd file and disable any enumerations of identical type)

  • scomp : compiles the schema into a jar file, which means it generates a pretty nice reusable library for you which contains a class (or multiple classes) based on the namespace of the schema (or starting with noNamespace.something) if you didn't define any in your XML

    EXAMPLE

    scomp -jar yourschema mySchema.xsd

    (which creates xmltypes.jar - which is the jar you want, just rename it to something clever and use it for two-way parsing for getting & setting, reading & writing values)

  • lastly, just launch your java IDE and add all the xmlbeans JARS and JAR for your schema created by scomp

You can then load,create,store,manipulate, with your xmldata as it would just be java objects in your projects ;-)

Based on the Element of your small XML snippet, you'd be accessing with code that looks roughly as follows:

 MyModel myModel = null;
 try {
    //full path to XML file
    File modelXML = new File("Model.xml");

    // Bind the incoming XML to an XMLBeans type.
    myModel = MyModel.Factory.parse(modelXML);
 } 
 catch (XmlException e) { e.printStackTrace(); } 
 catch (IOException e) { e.printStackTrace(); }
 catch (FileNotFoundException e) { e.printStackTrace();  }

 // Using the <structure> element.
 Structure structure = myModel.getStructure();
 Member member = structure.getMember();
 String target = member.getTarget(); // "PositiveIntegerListDefinition" 

NOTE: Something I'd also suggest is using Element names that are not reserved Java keywords such as list, I'm not exactly sure how XMLBeans will handle it but I'm assuming it would either rename it slightly rather than overload.

Here's a full tutorial which could come in handy: http://xmlbeans.apache.org/documentation/tutorial_getstarted.html

I made the assumption you are using simple direct XML calling or REST-based Web Services, but if you decided to use something more verbose like SOAP there are also Apache CXF and Apache Axis2 which can easily generate Java classes from SOAP XML messages or WSDLs.

bcmoney
  • 2,889
  • 1
  • 24
  • 30
  • this answers my question. I think I am going to go with XMLBeans. More googling tells me there is also JAXB 2, wondering which is better. – Vineet Oct 31 '12 at 02:53
  • Seems a discussion/question around this already happened too: http://stackoverflow.com/questions/1362030/jaxb-vs-apache-xmlbeans Some prefer JAXB 2 because it is native Java but some still find use for XMLBeans for some minor performance improvements and additional features, its really a matter of personal preference of whether you think its worth using an external 3rd party library or not. If I had a small personal project I would almost always use XMLBeans, but if it was a larger complex enterprise one I might think twice before adding dependencies and just stick with JAXB. – bcmoney Nov 01 '12 at 15:46