使用Gson将XML文件转换为Json

时间:2021-01-18 21:46:14

I found that many of the tutorials on the net and here on SO also refer to net.sf.json library to convert an xml file to json object.

我发现网上的许多教程和SO上的这些教程也引用了net.sf.json库来将xml文件转换为json对象。

But, I want an alternative preferably using Gson. Is it possible? I don't have well defined Java Classes for the XML file. But, I just want to convert the xml file to com.google.gson.JsonObject. How to achieve it?

但是,我想要一个替代方案,最好使用Gson。可能吗?我没有为XML文件定义良好的Java类。但是,我只想将xml文件转换为com.google.gson.JsonObject。怎么实现呢?

1 个解决方案

#1


1  

I've done the same using JAXB to convert my xml to an object, and passing the object to gson. I know it takes one additional step, but that worked convenient for me. Upon converting xml to jaxb see also: Use JAXB to create Object from XML String

我已经使用JAXB将我的xml转换为对象,并将对象传递给gson。我知道这需要一个额外的步骤,但这对我来说很方便。在将xml转换为jaxb时,请参阅:使用JAXB从XML String创建Object

#1


1  

I've done the same using JAXB to convert my xml to an object, and passing the object to gson. I know it takes one additional step, but that worked convenient for me. Upon converting xml to jaxb see also: Use JAXB to create Object from XML String

我已经使用JAXB将我的xml转换为对象,并将对象传递给gson。我知道这需要一个额外的步骤,但这对我来说很方便。在将xml转换为jaxb时,请参阅:使用JAXB从XML String创建Object