{"id":41,"date":"2011-10-28T03:01:39","date_gmt":"2011-10-28T03:01:39","guid":{"rendered":"http:\/\/hhs1080.hosting.paran.com\/?p=41"},"modified":"2011-10-28T03:01:39","modified_gmt":"2011-10-28T03:01:39","slug":"how-to-convert-xml-to-json-in-java","status":"publish","type":"post","link":"https:\/\/blog.goorigo.com\/?p=41","title":{"rendered":"How to Convert XML to JSON in Java"},"content":{"rendered":"<p><a href=\"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\/<\/a><\/p>\n<p>Use json-lib, a library which adds JSON support to any Java program. json-lib can take XML and convert it into a JSON object.<\/p>\n<p>1. Add these dependencies to your project:<\/p>\n<pre>&lt;dependency&gt;\n? ? ? ? &lt;groupId&gt;net.sf.json-lib&lt;\/groupId&gt;\n? ? ? ? &lt;artifactId&gt;json-lib&lt;\/artifactId&gt;\n? ? ? ? &lt;version&gt;2.3&lt;\/version&gt;\n? ? ? ? &lt;type&gt;jar&lt;\/type&gt;\n? ? ? ? &lt;classifier&gt;jdk15&lt;\/classifier&gt;\n? ? ? ? &lt;scope&gt;compile&lt;\/scope&gt;\n? ? &lt;\/dependency&gt;\n? ? &lt;dependency&gt;\n? ? ? ? &lt;groupId&gt;org.apache.commons&lt;\/groupId&gt;\n? ? ? ? &lt;artifactId&gt;commons-io&lt;\/artifactId&gt;\n? ? ? ? &lt;version&gt;1.3.2&lt;\/version&gt;\n? ? ? ? &lt;type&gt;jar&lt;\/type&gt;\n? ? ? ? &lt;scope&gt;compile&lt;\/scope&gt;\n? ? &lt;\/dependency&gt;\n? ? &lt;dependency&gt;\n? ? ? ? &lt;groupId&gt;xom&lt;\/groupId&gt;\n? ? ? ? &lt;artifactId&gt;xom&lt;\/artifactId&gt;\n? ? ? ? &lt;version&gt;1.1&lt;\/version&gt;\n? ? &lt;\/dependency&gt;\n? &lt;\/dependencies&gt;<\/pre>\n<p>What does this mean? See How to Add a Dependency to a Java Project<\/p>\n<p>2. Put the following XML sample in your classpath in a file named sample-xml.xml:<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;important-data certified=\"true\" processed=\"true\"&gt;\n? &lt;timestamp&gt;232423423423&lt;\/timestamp&gt;\n? &lt;authors&gt;\n? ? &lt;author&gt;\n? ? ? &lt;firstName&gt;Tim&lt;\/firstName&gt;\n? ? ? &lt;lastName&gt;Leary&lt;\/lastName&gt;\n? ? &lt;\/author&gt;\n? &lt;\/authors&gt;\n? &lt;title&gt;Flashbacks&lt;\/title&gt;\n? &lt;shippingWeight&gt;1.4 pounds&lt;\/shippingWeight&gt;\n? &lt;isbn&gt;978-0874778700&lt;\/isbn&gt;\n&lt;\/important-data&gt;<\/pre>\n<p>3. Load the resource from the classpath and parse this XML into JSON as follows:<\/p>\n<pre>package com.discursive.answers;\n\nimport java.io.InputStream;\n\nimport net.sf.json.JSON;\nimport net.sf.json.xml.XMLSerializer;\n\nimport org.apache.commons.io.IOUtils;\n\npublic class ConvertXMLtoJSON {\n\n? ? ? ? public static void main(String[] args) throws Exception {\n? ? ? ? ? ? ? ? InputStream is = \n? ? ? ? ? ? ? ? ? ? ? ? ConvertXMLtoJSON.class.getResourceAsStream(\"sample-xml.xml\");\n? ? ? ? ? ? ? ? String xml = IOUtils.toString(is);\n? ? ? ? ? ? ? ? \n? ? ? ? ? ? ? ? XMLSerializer xmlSerializer = new XMLSerializer(); \n? ? ? ? ? ? ? ? JSON json = xmlSerializer.read( xml ); ?\n? ? ? ? ? ? ? ? System.out.println( json.toString(2) );\n? ? ? ? }\n}<\/pre>\n<p>4. This should produce the following JSON output:<\/p>\n<pre>{\n? \"@certified\": \"true\",\n? \"@processed\": \"true\",\n? \"timestamp\": \"232423423423\",\n? \"authors\": [ ?{\n? ? \"firstName\": \"Tim\",\n? ? \"lastName\": \"Leary\"\n? }],\n? \"title\": \"Flashbacks\",\n? \"shippingWeight\": \"1.4 pounds\",\n? \"isbn\": \"978-0874778700\"\n}<\/pre>\n<p>This sample project is available on GitHub here:?<a title=\"External link\" href=\"http:\/\/github.com\/tobrien\/sample-json-parsing\">http:\/\/github.com\/to&#8230;le-json-parsing<\/a><\/p>\n<p>For more information about the json-lib project, see?<a title=\"External link\" href=\"http:\/\/json-lib.sourceforge.net\/\">JSON-LIB project page<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/answers.oreilly.com\/topic\/278-how-to-convert-xml-to-json-in-java\/ Use json-lib, a library which adds JSON support to any Java program. json-lib can take XML and convert it into a JSON object. 1. Add these dependencies to your&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/posts\/41"}],"collection":[{"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=41"}],"version-history":[{"count":0,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/posts\/41\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}