是否存在通过Java从XML模式生成JSON模式的工具?

时间:2021-10-28 17:17:27

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?

有没有人知道一种工具或方法,我们可以从XML模式或Java从JSON模式中生成JSON模式?

5 个解决方案

#1


17  

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.

它不是很优雅,但是jackson可以从java类生成json模式。因此,您可以使用xml模式,使用jaxb注释从它生成java类,然后从中生成json模式,因为jackson支持jaxb注释。

#2


5  

If you can get POJOs that match Schema (using xjc for example), you could then use Jackson to produce JSON Schema (see ObjectMapper.generateSchema(...)).

如果您可以获得与模式匹配的pojo(例如使用xjc),那么您可以使用Jackson来生成JSON模式(参见ObjectMapper.generateSchema(…))。

#3


4  

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.

我希望您能够编写一个XSLT脚本,它可以相当容易地从XML模式生成JSON结构。这之所以有效,是因为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. 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.

走另一条路会更困难一点。您需要一些能够读取JSON模式并输出XML的东西。为此,您需要一个解析器,可能还需要构建解析的AST。通过这种方法和树遍历,您很可能能够很容易地生成XML模式。

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.

ANTLR将允许您定义语法、构建解析器和AST。它有某种“结构化文本”生成器,可以用于执行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.

我们的DMS软件再工程工具包就像ANTLR,但是有更多的机器。使用DMS,您可以定义JSON语法、构建ast,然后编写源到源的tranformatinos,将其映射到表示模式的XML。

#4


2  

for example CXF can serve request in Json or XML so it must have a way to convert the two.

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

#5


2  

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://www.json.org/javadoc/org/json/XML.html#toJSONObject%28java.lang.String%29

http://answers.oreilly.com/topic/278-how-to-convert-xml-to-json-in-java http://www.json.org/javadoc/org/json/XML.html toJSONObject % 28以% 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://code.google.com/p/xml2json-xslt/
http://json-lib.sourceforge.net/index.html

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

还有http://x-stream.github。io/ library,允许您在两边进行转换(也可以转换为pojo)。这里有一个简单的示例用法:将XML转换为JSON格式

/edit: ups, looks like I didn't understood question correctly :P

/编辑:ups,好像我没听懂问题:P

#1


17  

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.

它不是很优雅,但是jackson可以从java类生成json模式。因此,您可以使用xml模式,使用jaxb注释从它生成java类,然后从中生成json模式,因为jackson支持jaxb注释。

#2


5  

If you can get POJOs that match Schema (using xjc for example), you could then use Jackson to produce JSON Schema (see ObjectMapper.generateSchema(...)).

如果您可以获得与模式匹配的pojo(例如使用xjc),那么您可以使用Jackson来生成JSON模式(参见ObjectMapper.generateSchema(…))。

#3


4  

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.

我希望您能够编写一个XSLT脚本,它可以相当容易地从XML模式生成JSON结构。这之所以有效,是因为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. 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.

走另一条路会更困难一点。您需要一些能够读取JSON模式并输出XML的东西。为此,您需要一个解析器,可能还需要构建解析的AST。通过这种方法和树遍历,您很可能能够很容易地生成XML模式。

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.

ANTLR将允许您定义语法、构建解析器和AST。它有某种“结构化文本”生成器,可以用于执行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.

我们的DMS软件再工程工具包就像ANTLR,但是有更多的机器。使用DMS,您可以定义JSON语法、构建ast,然后编写源到源的tranformatinos,将其映射到表示模式的XML。

#4


2  

for example CXF can serve request in Json or XML so it must have a way to convert the two.

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

#5


2  

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://www.json.org/javadoc/org/json/XML.html#toJSONObject%28java.lang.String%29

http://answers.oreilly.com/topic/278-how-to-convert-xml-to-json-in-java http://www.json.org/javadoc/org/json/XML.html toJSONObject % 28以% 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://code.google.com/p/xml2json-xslt/
http://json-lib.sourceforge.net/index.html

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

还有http://x-stream.github。io/ library,允许您在两边进行转换(也可以转换为pojo)。这里有一个简单的示例用法:将XML转换为JSON格式

/edit: ups, looks like I didn't understood question correctly :P

/编辑:ups,好像我没听懂问题:P