Marco Sulla
Marco Sulla

Reputation: 15930

Is there a way to create a .java class from a XML schema using Java code and NOT an external program?

I first explain my real problem.

I have a XML and the relative XML schema (an XSD). I want to transform it in JSON. XML and its schema can change, so I have to do it at runtime.

Now, the XSD can be a very complex type. I know that exists libraries that transform XML to JSON directly, but I don't think they will work well with complicated structures.

My intention was to use Jackson, my favorite JSON library. But Jackson needs a Java class and a Java object to serialize the object as JSON.

So I thought about JAXB. With JAXB I can create a .java from a XML schema.

After that, I can load the class at runtime and, with JAXB, create an instance of the class using the XML. Then Jackson.

The problem is I have not found a single example, and I didn't find nothing in JAXB API docs too, of how to use JAXB to convert a XML schema to a java class using its API. All examples suggest to use external programs to generate the classes. I don't want to do this, since IMHO it's less portable.

Is there not a way to use JAXB, or another Java library, to convert an XML schema to a .java class using Java code and not an external tool?

Alternatively, is there not a way to convert XML to JSON, using the XML schema as source of its structure?

PS: I'm using Java Azul ZuluFx 8. I don't think it's relevant, but you never know.

Upvotes: 0

Views: 1167

Answers (4)

Valentyn Kolesnikov
Valentyn Kolesnikov

Reputation: 2097

Underscore-java library can read XML string to a map and generate JSON from the map. I am the maintainer of the project.

Msp<String, Object> map = U.fromXmlMap(xml);
String json = U.toJson(map);

Upvotes: 1

Miku
Miku

Reputation: 607

There are converters (xml to json) online (such as this one), so it doable.

However those are external programs. If you want something internal I'm afraid you'd need to write it yourself. Judging from question and your comments I assume you want it as a part of a program and not as a plugin for IDE (or I might be wrong, correct me otherwise)

XML to JSON should not be very difficult to do - read all nodes and child nodes recursively (how to read child nodes example) and create a json file from that

Making it from XML to java would be a bit more tricky and might require some assistance (depending on how you want to do it)

One quite naive approach would be to have it all as strings. So <name>Tom</name> would be String name = "Tom";, but 30 would be String age = "30"; as well. Anything more then "everything is a string" approach would require human intervention, since some elements can be numbers at first glance, but might be strings (e.g. buildings numbers: 1, 2, 3, 3a, 4, ...)

Upvotes: 0

zomega
zomega

Reputation: 2326

If you choose a XML parser that supports XSD validation it might also provide an API to query the information which is defined in the XSD. You could try xerces.

If there is no such API you will have to parse the XSD yourself. Create one parser instance to parse the XML and one to parse the XSD. The XSD structure is not so complicated so it should be possible to combine the information of the XML and the XSD.

Then simply write the information to a .java file using java.io.

Upvotes: 0

Stefan B.
Stefan B.

Reputation: 1

If you are using Eclipse you can simply right click on the schema, click generate and choose JAXB classes. Then follow the simple wizard and it will create Java classes for you.

Upvotes: 0

Related Questions