当前位置: 首页 > 工具软件 > XML to JSON > 使用案例 >

XmlToJson转换

郭知
2023-12-01

JAVA代码如下:

package com.tony;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class XmlToJson {

	public static void main(String[] args) {
		String xmlStr = null;
		try {
			xmlStr = FileUtils.readFileToString(new File(new XmlToJson().getClass().getClassLoader().getResource(".").getPath() + "data.xml"));
			String jsonStr = xmlToJson(xmlStr);
			System.out.println(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static String xmlToJson(String xml) {
		JSONObject obj = new JSONObject();
		try {
			InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
			SAXBuilder sb = new SAXBuilder();
			Document doc = sb.build(is);
			Element root = doc.getRootElement();
			obj.put(root.getName(), iterateElement(root));
			return obj.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private static Map iterateElement(Element element) {
		List jiedian = element.getChildren();
		Element et = null;
		Map obj = new HashMap();
		List list = new LinkedList();
		int num = 0;
		for (int i = 0; i < jiedian.size(); i++) {
			et = (Element) jiedian.get(i);
			if (et.getTextTrim().equals("")) {
				if (et.getChildren().size() == 0)
					continue;
				if (obj.containsKey(et.getName())) {
					++num;
					if (num == 1)
						list.add(obj.get(et.getName()));
					list.add(iterateElement(et));
					obj.put(et.getName(), list);
				} else {
					obj.put(et.getName(), iterateElement(et));
				}
			} else {
				obj.put(et.getName(), et.getTextTrim());
			}
		}
		return obj;
	}

}

xml文档内容如下:


<?xml version="1.0" encoding="utf-8" ?>
<items>
	<item>
		<url>www.csdn.net</url>
		<image>demo.jpg</image>
		<title>Ajax无刷新翻页</title>
		<desc>freejs已经发了不少无刷新翻页的代码了</desc>
	</item>
	<item>
		<url>www.ceshi.com</url>
		<image>csshi.jpg</image>
		<title>代码</title>
		<desc>源码</desc>
	</item>
	<item>
		<url>www.xiaomi.com</url>
		<image>xiaomi.jpg</image>
		<title>类似京东和易迅的菜单</title>
		<desc>演示地址可以存源码</desc>
	</item>
	<item>
		<url>http://www.baidu.com</url>
		<image>baidu.jpg</image>
		<title>实现简单的Tab切换菜单</title>
		<desc>切换菜单</desc>
	</item>
	<item>
		<url>freejs</url>
		<image>logo.png</image>
		<title>演示导航菜单,TAB标签</title>
		<desc>导航菜单</desc>
	</item>
	<itemChildren>
		<id>136</id>
		<name>张建江</name>
	</itemChildren>
</items>  

转换结果:

{
    "items": {
        "item": [
            {
                "desc": "freejs已经发了不少无刷新翻页的代码了",
                "image": "demo.jpg",
                "title": "Ajax无刷新翻页",
                "url": "www.csdn.net"
            },
            {
                "desc": "源码",
                "image": "csshi.jpg",
                "title": "代码",
                "url": "www.ceshi.com"
            },
            {
                "desc": "演示地址可以存源码",
                "image": "xiaomi.jpg",
                "title": "类似京东和易迅的菜单",
                "url": "www.xiaomi.com"
            },
            {
                "desc": "切换菜单",
                "image": "baidu.jpg",
                "title": "实现简单的Tab切换菜单",
                "url": "http://www.baidu.com"
            },
            {
                "desc": "导航菜单",
                "image": "logo.png",
                "title": "演示导航菜单,TAB标签",
                "url": "freejs"
            }
        ],
        "itemChildren": {
            "id": "136",
            "name": "张建江"
        }
    }
}

如有测试问题欢迎砖拍.....

 类似资料: