0

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.

Marco Sulla
  • 15,299
  • 14
  • 65
  • 100
  • It seems, you are looking for this https://stackoverflow.com/a/7248944/1162620 – Ravi Jul 31 '19 at 09:57
  • If I'm not wrong, you are asking, if you could generate java class using java code. If that is the thing, then even my comment remain same. What this **not** external library, that's part of JDK – Ravi Jul 31 '19 at 10:09
  • @Ravi I ***explicitly*** asked about do the think ***without*** external programs, but using only Java code. – Marco Sulla Jul 31 '19 at 10:11
  • @Ravi You can post Java code or simply point me to the API documentation of a library that does what I'm searching for. – Marco Sulla Jul 31 '19 at 10:13
  • I already pointed one library for you – Ravi Jul 31 '19 at 10:14
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/197262/discussion-between-marco-sulla-and-ravi). – Marco Sulla Jul 31 '19 at 10:14
  • Sorry, I don't know anything else, which could help you.. So, not point of having further discussion. Thanks. – Ravi Jul 31 '19 at 10:17
  • @Ravi: It's incredible... you downvoted my question and asked for close because is unclear. My question is ***very*** clear: I want to do what you linked, but not with that external program, but using Java. Ok? – Marco Sulla Jul 31 '19 at 10:19
  • @Ravi Furthermore, I kindly asked you to continue the discussion in a private chat, where I tried to explain you my point. You avoided it completely. I have no words. – Marco Sulla Jul 31 '19 at 10:27
  • You are always going to use JDK, if you use java, then can you justify, how can be using JDK library would be less portable **I don't want to do this, since IMHO it's less portable.** ? And, you are expecting us to write some code, if no, then show us your code, instead of writing long question. Be specific. And that's the reason, I downvoted. If that worries you then edit you question so that I will revert my vote. Thanks – Ravi Jul 31 '19 at 10:45
  • @Ravi: the link you posted shows an example that uses an external program, not a JDK library. Maybe you wrong the link, I don't know. Anyway, I can't post the code since I have nothing to post! I don't know how to do this thing, and posting the previous code that does something completely unrelated it's useless. I think I was very specific, I described my real problem in details and I asked for a solution. I never asked about code. I repeat, if you want to post the link to the API I need, as somega did, you're free to do it. And I don't really know how can I improve my question, seriously. – Marco Sulla Jul 31 '19 at 13:15

4 Answers4

1

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);
Valentyn Kolesnikov
  • 2,029
  • 1
  • 24
  • 31
0

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.

Stefan B.
  • 1
  • 1
0

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.

zomega
  • 1,538
  • 8
  • 26
0

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, ...)

Miku
  • 567
  • 6
  • 15