Java tutorial
//package com.java2s; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { public static String start = "<![CDATA["; public static String end = "]]>"; public static Map<String, Object> toMap(String xml) { Map<String, Object> m = new HashMap<>(); m.put("attributes", getAttributes(xml)); m.put("element name", getWrapperName(xml)); if (getWrapperValue(xml).indexOf("<") == -1) { m.put(getWrapperName(xml), getWrapperValue(xml)); return m; } xml = getWrapperValue(xml); List<String> split = split(xml); for (String s : split) { String value = getWrapperValue(s); String name = getWrapperName(s); if (!value.startsWith("<") && !value.startsWith(start)) { m.put(name, value); } else if (value.startsWith(start)) { m.put(name, replaceEscape(value)); } else if (value.startsWith("<") && !isList(value)) { m.put(name, toMap(s)); } else if (isList(value)) { m.put(name, toList(value)); } } return m; } private static Map<String, String> getAttributes(String xml) { String s = xml.substring(xml.indexOf("<") + 1, xml.indexOf(">")); String[] ss = s.split(" "); if (ss.length == 1) { return null; } Map<String, String> m = new HashMap<>(); for (int i = 1; i < ss.length; i++) { String s0 = ss[i].trim(); if (!s0.isEmpty()) { m.put(s0.split("=")[0], delQuot(s0.split("=")[1])); } } return m; } private static String getWrapperName(String xml) { return xml.substring(xml.indexOf("<") > -1 ? (xml.indexOf("<") + 1) : 0, xml.indexOf(">") > -1 ? xml.indexOf(">") : xml.length()).split(" ")[0]; } private static String getWrapperValue(String xml) { return xml.substring(xml.indexOf(">") > -1 ? (xml.indexOf(">") + 1) : 0, xml.lastIndexOf("<") > -1 ? xml.lastIndexOf("<") : xml.length()); } private static List<String> split(String xml) { int i = 0; int left = 0;// < int right = 0;// > int close = 0;// </ left+close=right; char[] cs = xml.toCharArray(); List<Integer> ls = new ArrayList<Integer>(); while (i < cs.length) { switch (cs[i]) { case '<': if (i == 0 || xml.indexOf(start, i - 1) != i) { if (cs[i + 1] != '/') { left++; } else { close++; } } else if (xml.indexOf(start, i - 1) == i) { i = xml.indexOf(end, i) + 3; continue; } break; case '>': if (cs[i - 1] != ']' && cs[i - 2] != ']') { right++; } if ((left + close) == right && left == close) { ls.add(i); } break; default: break; } i++; } List<String> ss = new ArrayList<String>(); if (ls.size() == 0) { ss.add(xml); } else if (ls.size() == 1) { ss.add(xml.substring(0, ls.get(0))); ss.add(xml.substring(ls.get(0) + 1)); } else if (ls.size() > 1) { ss.add(xml.substring(0, ls.get(0) + 1)); for (int j = 0; j < ls.size() - 1; j++) { ss.add(xml.substring(ls.get(j) + 1, ls.get(j + 1) + 1)); } } return ss; } private static String replaceEscape(String xml) { return xml.substring(xml.indexOf(start) + 9, xml.lastIndexOf(end)); } private static boolean isList(String xml) { String n1 = getWrapperName(xml); if (xml.indexOf("</" + n1 + ">") == -1) { return false; } else if (xml.substring(xml.indexOf("</" + n1 + ">")).indexOf("<" + n1 + ">") > -1) { return true; } return false; } private static List<Object> toList(String xml) { List<Object> ls = new ArrayList<>(); for (String s : split(xml)) { if (isList(s)) { ls.add(toList(s)); } else { ls.add(toMap(s)); } } return ls; } private static String delQuot(String s) { return s.substring(s.indexOf('"') + 1, s.lastIndexOf('"')); } }