2 * Copyright 2016 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openo.carbon.bpel.util;
18 import java.io.ByteArrayInputStream;
20 import java.io.InputStream;
21 import java.util.HashMap;
22 import java.util.LinkedList;
23 import java.util.List;
26 import net.sf.json.JSONObject;
28 import org.jdom.Document;
29 import org.jdom.Element;
30 import org.jdom.input.SAXBuilder;
32 public class Xml2JsonUtil {
34 * transform xml to json
36 * @param xml xml format string
37 * @return return json string when success; otherwise return null
39 public static String xml2JSON(String xml) {
40 JSONObject obj = new JSONObject();
42 InputStream is = new ByteArrayInputStream(xml.getBytes("utf-8"));
43 SAXBuilder sb = new SAXBuilder();
44 Document doc = sb.build(is);
45 Element root = doc.getRootElement();
46 obj.put(root.getName(), iterateElement(root));
47 return obj.toString();
48 } catch (Exception e) {
55 * transform xml file to json string
57 * @param file java.io.File is an effective xml file
58 * @return return json string when success; otherwise return null
60 public static String xml2JSON(File file) {
61 JSONObject obj = new JSONObject();
63 SAXBuilder sb = new SAXBuilder();
64 Document doc = sb.build(file);
65 Element root = doc.getRootElement();
66 obj.put(root.getName(), iterateElement(root));
67 return obj.toString();
68 } catch (Exception e) {
75 * an iteration function
77 * @param parentElement : org.jdom.Element
78 * @return java.util.Map
80 @SuppressWarnings({"unchecked", "rawtypes"})
81 private static Map iterateElement(Element parentElement) {
82 List node = parentElement.getChildren();
83 Element element = null;
84 Map map = new HashMap();
86 for (int i = 0; i < node.size(); i++) {
87 element = (Element) node.get(i);
88 if (element.getTextTrim().equals("")) {
89 if (element.getChildren().size() == 0)
91 if (map.containsKey(element.getName())) {
92 Object obj = map.get(element.getName());
93 if (obj instanceof Map) {
94 list = new LinkedList();
96 list.add(iterateElement(element));
97 map.remove(element.getName());
98 map.put(element.getName(), list);
99 } else if (obj instanceof List) {
101 list.add(iterateElement(element));
104 map.put(element.getName(), iterateElement(element));
107 map.put(element.getName(), element.getTextTrim());
113 public static void main(String[] args) {
114 System.out.println(Xml2JsonUtil.xml2JSON("<MapSet>" + "<MapGroup id='Sheboygan'>" + "<Map>"
115 + "<Type>MapGuideddddddd</Type>" + "<SingleTile>true</SingleTile>" + "<Extension>"
116 + "<ResourceId>ddd</ResourceId>" + "</Extension>" + "</Map>" + "<Map>" + "<Type>ccc</Type>"
117 + "<SingleTile>ggg</SingleTile>" + "<Extension>" + "<ResourceId>aaa</ResourceId>"
118 + "</Extension>" + "</Map>" + "<Extension />" + "</MapGroup>" + "<ddd>" + "33333333"
119 + "</ddd>" + "<ddd>" + "444" + "</ddd>" + "</MapSet>"));
122 "<body> <p:helloXsl xmlns:p=\"http://ode/bpel/unit-test.wsdl\"> <!--Exactly 1 occurrence--> <TestPart> <!--Exactly 1 occurrence--> <content>fdsafasdfasdf</content> </TestPart> </p:helloXsl></body>";
124 System.out.println(Xml2JsonUtil.xml2JSON(xml));
127 "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:unit=\"http://ode/bpel/unit-test.wsdl\"> <soapenv:Header/> <soapenv:Body> <unit:helloXsl> <TestPart> <content>?</content> </TestPart> </unit:helloXsl> </soapenv:Body></soapenv:Envelope>";
129 System.out.println(Xml2JsonUtil.xml2JSON(xml));
132 "<ns:uploadServiceResponse xmlns:ns=\"http://services.deployer.bpel.carbon.wso2.org\"><ns:return xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/></ns:uploadServiceResponse>";
134 System.out.println(Xml2JsonUtil.xml2JSON(xml));
137 "<body> <p:planInput xmlns:p=\"http://www.open-o.org/tosca/nfv/2015/12\"> <!--Exactly 1 occurrence--> <p:sfc_count>2</p:sfc_count> <!--Exactly 1 occurrence--> <p:iaUrl></p:iaUrl> <!--Exactly 1 occurrence--> <p:vnfmId>112</p:vnfmId> <!--Exactly 1 occurrence--> <p:object_context>{\"e\":{\"f\":\"4\"}}</p:object_context> <!--Exactly 1 occurrence--> <p:statusUrl></p:statusUrl> <!--Exactly 1 occurrence--> <p:serviceTemplateId>?</p:serviceTemplateId> <!--Exactly 1 occurrence--> <p:roUrl></p:roUrl> <!--Exactly 1 occurrence--> <p:vl_count>2</p:vl_count> <!--Exactly 1 occurrence--> <p:containerapiUrl>?</p:containerapiUrl> <!--Exactly 1 occurrence--> <p:flavor></p:flavor> <!--Exactly 1 occurrence--> <p:nsInstanceId>223</p:nsInstanceId> <!--Exactly 1 occurrence--> <p:instanceId>334</p:instanceId> <!--Exactly 1 occurrence--> <p:resourceUrl></p:resourceUrl> <!--Exactly 1 occurrence--> <p:vnf_count>2</p:vnf_count> <!--Exactly 1 occurrence--> <p:callbackId></p:callbackId> <!--Exactly 1 occurrence--> <p:object_additionalParamForVnf>[{\"b\":1},{\"c\":{\"d\":\"2\"}}}]</p:object_additionalParamForVnf> <!--Exactly 1 occurrence--> <p:object_additionalParamForNs>[{\"a\":3},{\"e\":{\"f\":\"4\"}}}]</p:object_additionalParamForNs> <!--Exactly 1 occurrence--> <p:flavorParams></p:flavorParams> </p:planInput></body>";
139 System.out.println(Xml2JsonUtil.xml2JSON(xml));