简体   繁体   中英

Is there a tool to generate a JSON schema from an XML schema through Java?

是否有人知道我们可以通过Java从XML模式或XML模式生成JSON模式的工具或方法?

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 .

如果您可以获得与Schema匹配的POJO(例如使用xjc),则可以使用Jackson生成JSON Schema(请参阅ObjectMapper.generateSchema(...) )。

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.

例如,CXF可以在Json或XML中提供请求,因此它必须有一种方法来转换这两者。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM