5

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.

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?

phoenix
  • 985
  • 3
  • 17
  • 38
  • 1
    Could you show what you have already tried? – C R Dec 25 '14 at 16:15
  • I have no standalone example, but recently done something similar with Jackson. If you are fine with GPL v3 (with exceptions), you might like the Xml2Json class in the org.knime.json.source bundle. (Jackson has probably something to convert to gson.) – Gábor Bakos Dec 25 '14 at 16:19
  • @rozkosz I have achieved the same using net.sf.json. I have read the API of Gson, JsonParser. 'JsonElement element = new JsonParser().parse(new FileReader("D:\\myXML.xml"));' doesn't seem to work. – phoenix Dec 25 '14 at 16:23

1 Answers1

2

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

Community
  • 1
  • 1
Michallis
  • 52
  • 4