How to generate JAXB classes from just XML

JavaXmlJaxb

Java Problem Overview


I need to generate classes from xml that doesn't provide a schema. I understand this is near useless, but the fact is we have xml, it's structured, and we should be able to create a model from the xml. In the past I've done it by hand, but the current xml documents I am working with are quite large and my time would probably be better spent building something that does what I need. But, I am guessing it has already been done, and I just can't find it.

Any pointers?

Java Solutions


Solution 1 - Java

There are many tools available (a quick google search should fetch you some) that can generate XSD from XML assuming string type for almost everything. You should be able to use that XSD to run JAXB to get classes.

Here's an online tool that lets you do that.

And here is a screen cap: enter image description here

Solution 2 - Java

From your xml file, you can create a XML Schema Definition (XSD) file. Once you have the XSD, you'll be able to generate the code, need it be for java, C#, C++, or all of the above.

If you have Visual Studio, you can use xsd.exe to generate the XSD file.

References:

XSD to Java: Reference:

XSD to C++: References:

XSD to C#: Reference:

  • quickstart.developerfusion.co.uk/quickstart/howto/doc/xmlserialization/XSDToCls.aspx
  • Command Syntax: "C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64\xsd.exe" -c -l:c# -n:SomeNameSpace example.xsd

Solution 3 - Java

If the XML was created by JAXB, it can easily be converted back into objects. There's a tutorial over at oracle which illustrates one way to do this. Spring framework offers similiar features using JAXB which are very conveniant.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanInDCView Question on Stackoverflow
Solution 1 - JavaBala RView Answer on Stackoverflow
Solution 2 - JavaJames OravecView Answer on Stackoverflow
Solution 3 - JavaJohan SjöbergView Answer on Stackoverflow