Is anyone aware of a tool or approach from which we can generate a JSON schema from XML schema or XML schema from JSON schema by Java?
问题:
回答1:
It isn't very elegant, but jackson can generate json schema from a java class . So you could take your xml schema, generate java classes from it with jaxb annotations, then generate the json schema from that as jackson supports jaxb annotations.
回答2:
If you can get POJOs that match Schema (using xjc for example), you could then use Jackson to produce JSON Schema (see ObjectMapper.generateSchema(...)).
回答3:
I'd expect you to be able to write an XSLT script that generated JSON structures from the XML schema fairly easily. This works because XSLT is prefectly happy to read and transform XML, and an XML schema is just XML.
Going the other way would be a bit harder. You need somthing that can read a JSON schema and spit out XML. For this you need a parser, and likely something that builds an AST of the parse. With that and a tree walk you're likely to be able to generate an XML schema fairly easily.
ANTLR will let you define grammars, build a parser and an AST fairly easily. It has some kind of "Structured Text" generator that might work for walking the AST.
Our DMS Software Reengineering Toolkit is like ANTLR but with more machinery. With DMS you can define the JSON syntax, build ASTs, and then write source-to-source tranformatinos to map that to XML representing your schema.
回答4:
for example CXF can serve request in Json or XML so it must have a way to convert the two.
回答5:
Converting XML to JSON is quite easy and can be done various ways:
http://answers.oreilly.com/topic/278-how-to-convert-xml-to-json-in-java
http://www.json.org/javadoc/org/json/XML.html#toJSONObject%28java.lang.String%29
For converting from XML to JSON look at this maybe, seems to be very simple:
http://www.bramstein.com/projects/xsltjson/
http://code.google.com/p/xml2json-xslt/
http://json-lib.sourceforge.net/index.html
There is also http://x-stream.github.io/ library which allows you to make conversion in both sides (also to POJOs). Simple example usage you can find here: Convert XML to JSON format
/edit: ups, looks like I didn't understood question correctly :P