使用Gson将XML文件转换为Json [英] Convert an XML file to Json using using Gson

查看:495
本文介绍了使用Gson将XML文件转换为Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

推荐答案

我已经使用JAXB将xml转换为对象,并将该对象传递给gson,完成了相同的操作. 我知道还需要采取其他步骤,但这对我来说很方便. 将xml转换为jaxb后,另请参见: 使用JAXB从XML字符串创建对象

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

这篇关于使用Gson将XML文件转换为Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆