简体   繁体   English

是否有工具通过Java从XML模式生成JSON模式?

[英]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 . 它不是很优雅,但是jackson可以从java类生成json模式 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 . 因此,您可以使用xml架构, 使用jaxb注释从中生成java类 ,然后从中生成json架构,因为jackson支持jaxb注释

如果您可以获得与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. 我希望您能够编写一个XSLT脚本,该脚本可以非常轻松地从XML模式生成JSON结构。 This works because XSLT is prefectly happy to read and transform XML, and an XML schema is just XML. 这是有效的,因为XSLT非常乐于阅读和转换XML,而XML模式只是XML。

Going the other way would be a bit harder. 走另一条路会有点困难。 You need somthing that can read a JSON schema and spit out XML. 您需要能够读取JSON模式并吐出XML的东西。 For this you need a parser, and likely something that builds an AST of the parse. 为此,您需要一个解析器,并且可能需要构建解析的AST。 With that and a tree walk you're likely to be able to generate an XML schema fairly easily. 通过它和树步行,您可以非常轻松地生成XML模式。

ANTLR will let you define grammars, build a parser and an AST fairly easily. ANTLR将让您轻松定义语法,构建解析器和AST。 It has some kind of "Structured Text" generator that might work for walking the AST. 它有某种“结构化文本”生成器,可以用于行走AST。

Our DMS Software Reengineering Toolkit is like ANTLR but with more machinery. 我们的DMS软件再造工具包就像ANTLR,但有更多的机器。 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. 使用DMS,您可以定义JSON语法,构建AST,然后编写源到源转换器以将其映射到表示您的架构的XML。

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

Converting XML to JSON is quite easy and can be done various ways: 将XML转换为JSON非常简单,可以通过多种方式完成:

http://answers.oreilly.com/topic/278-how-to-convert-xml-to-json-in-java 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 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: 对于从XML转换为JSON的看法,这看起来很简单:

http://www.bramstein.com/projects/xsltjson/ http://www.bramstein.com/projects/xsltjson/
http://code.google.com/p/xml2json-xslt/ http://code.google.com/p/xml2json-xslt/
http://json-lib.sourceforge.net/index.html 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). 还有http://x-stream.github.io/库,允许您在两侧进行转换(也适用于POJO)。 Simple example usage you can find here: Convert XML to JSON format 您可以在此处找到简单的示例用法: 将XML转换为JSON格式

/edit: ups, looks like I didn't understood question correctly :P /编辑:ups,看起来我没有正确理解问题:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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