Java XML to JSON format

Source: Internet
Author: User

  Tags: xml to json JSON2014-05-20 20:55 6568 People read Comments (6) favorite reports Classification:"J2SE Foundation" (a)

The code looks like this, and the importance of the codebase is found in this example, and if you build your own code base, you can get it out of your own code base, so you can take whatever you need to get out of it at any time. The codebase is guaranteed to be normative and correct, and has simple module annotations.

This is today on the Internet to find the XML to JSON format code, others packaged quickly, saving me a lot of time ah.

[Java]View PlainCopyprint?
  1. Import Java.io.ByteArrayInputStream;
  2. Import Java.io.File;
  3. Import Java.io.InputStream;
  4. Import Java.util.HashMap;
  5. Import java.util.LinkedList;
  6. Import java.util.List;
  7. Import Java.util.Map;
  8. Import Net.sf.json.JSONObject;
  9. Import org.jdom.Document;
  10. Import org.jdom.Element;
  11. Import Org.jdom.input.SAXBuilder;
  12. Public class Xml2jsonutil {
  13. /** 
  14. * Convert a string in XML format to JSON format
  15. *
  16. * @param xml
  17. * String in XML format
  18. * @return successfully returned a JSON-formatted string;
  19. */
  20. @SuppressWarnings ("unchecked")
  21. public static string Xml2json (String xml) {
  22. Jsonobject obj = new Jsonobject ();
  23. try {
  24. InputStream is = new Bytearrayinputstream (Xml.getbytes ("Utf-8"));
  25. Saxbuilder sb = new Saxbuilder ();
  26. Document doc = Sb.build (IS);
  27. Element root = Doc.getrootelement ();
  28. Obj.put (Root.getname (), iterateelement (root));
  29. return obj.tostring ();
  30. } catch (Exception e) {
  31. E.printstacktrace ();
  32. return null;
  33. }
  34. }
  35. /** 
  36. * Convert a string in XML format to JSON format
  37. *
  38. * @param file
  39. * Java.io.File instance is a valid XML file
  40. * @return successfully reversed back to JSON-formatted string;
  41. */
  42. @SuppressWarnings ("unchecked")
  43. public static String Xml2json (file file) {
  44. Jsonobject obj = new Jsonobject ();
  45. try {
  46. Saxbuilder sb = new Saxbuilder ();
  47. Document doc = sb.build (file);
  48. Element root = Doc.getrootelement ();
  49. Obj.put (Root.getname (), iterateelement (root));
  50. return obj.tostring ();
  51. } catch (Exception e) {
  52. E.printstacktrace ();
  53. return null;
  54. }
  55. }
  56. /** 
  57. * An iterative approach
  58. *
  59. * @param element
  60. *: Org.jdom.Element
  61. * @return Java.util.Map instances
  62. */
  63. @SuppressWarnings ("unchecked")
  64. private static Map iterateelement (element Element) {
  65. List Jiedian = Element.getchildren ();
  66. Element et = null;
  67. Map obj = new HashMap ();
  68. List List = null;
  69. For (int i = 0; i < jiedian.size (); i++) {
  70. List = new LinkedList ();
  71. ET = (Element) jiedian.get (i);
  72. if (Et.gettexttrim (). Equals ("")) {
  73. if (Et.getchildren (). Size () = = 0)
  74. continue;
  75. if (Obj.containskey (Et.getname ())) {
  76. List = (list) obj.get (Et.getname ());
  77. }
  78. List.add (Iterateelement (ET));
  79. Obj.put (Et.getname (), list);
  80. } Else {
  81. if (Obj.containskey (Et.getname ())) {
  82. List = (list) obj.get (Et.getname ());
  83. }
  84. List.add (Et.gettexttrim ());
  85. Obj.put (Et.getname (), list);
  86. }
  87. }
  88. return obj;
  89. }
  90. //Test
  91. public static void Main (string[] args) {
  92. System.out.println (Xml2jsonutil.xml2json ("<MapSet>"
  93. + "<mapgroup id= ' Sheboygan ' >" + "<Map> "
  94. + "<Type>MapGuideddddddd</Type>"
  95. + " <SingleTile>true</SingleTile>" + "<Extension>"
  96. + " <ResourceId>ddd</ResourceId>" + "</Extension>" + "</Map>"
  97. + " <Map>" + "<Type>ccc</Type>" + "<SingleTile>ggg</SingleTile>"
  98. + " <Extension>" + "<ResourceId>aaa</ResourceId>"
  99. + "</Extension>" + "</Map>" + "<extension/>" + "</MapGroup>"
  100. + "<ddd>" + "33333333" + "</ddd>" + "<ddd>" + "444" + "</ddd>"
  101. + "</MapSet>"));
  102. }
  103. }

Required jar package in my resource address: http://download.csdn.net/detail/lovesummerforever/7376755


http://blog.csdn.net/lovesummerforever/article/details/26396465

http://hanyi366.iteye.com/blog/1162365

Top
0
Step
0

Java XML to JSON format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.