2019独角兽企业重金招聘Python工程师标准>>>

6936d1565ae689371725cdb5fc51c415.png

package com.lieni.ruyu.api.xmlTool;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.util.List;

import org.dom4j.Document;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import com.alibaba.fastjson.JSONArray;

import com.alibaba.fastjson.JSONObject;

public class XmlToJson {

/**

* 将xml转换为JSON对象

*

* @param xml xml字符串

* @return

* @throws Exception

*/

public static JSONObject xmltoJson(String xml) throws Exception {

JSONObject jsonObject = new JSONObject();

Document document = DocumentHelper.parseText(xml);

// 获取根节点元素对象

Element root = document.getRootElement();

iterateNodes(root, jsonObject);

return jsonObject;

}

/**

* 遍历元素

*

* @param node 元素

* @param json 将元素遍历完成之后放的JSON对象

*/

@SuppressWarnings("unchecked")

public static void iterateNodes(Element node, JSONObject json) {

// 获取当前元素的名称

String nodeName = node.getName();

// 判断已遍历的JSON中是否已经有了该元素的名称

if (json.containsKey(nodeName)) {

// 该元素在同级下有多个

Object Object = json.get(nodeName);

JSONArray array = null;

if (Object instanceof JSONArray) {

array = (JSONArray) Object;

} else {

array = new JSONArray();

array.add(Object);

}

// 获取该元素下所有子元素

ListlistElement = node.elements();

if (listElement.isEmpty()) {

// 该元素无子元素,获取元素的值

String nodeValue = node.getTextTrim();

array.add(nodeValue);

json.put(nodeName, array);

return;

}

// 有子元素

JSONObject newJson = new JSONObject();

// 遍历所有子元素

for (Element e : listElement) {

// 递归

iterateNodes(e, newJson);

}

array.add(newJson);

json.put(nodeName, array);

return;

}

// 该元素同级下第一次遍历

// 获取该元素下所有子元素

ListlistElement = node.elements();

if (listElement.isEmpty()) {

// 该元素无子元素,获取元素的值

String nodeValue = node.getTextTrim();

json.put(nodeName, nodeValue);

return;

}

// 有子节点,新建一个JSONObject来存储该节点下子节点的值

JSONObject object = new JSONObject();

// 遍历所有一级子节点

for (Element e : listElement) {

// 递归

iterateNodes(e, object);

}

json.put(nodeName, object);

return;

}

/**

* 测试的main方法

*/

public static void main(String[] args) throws Exception {

String xml = "" + "" + " 查询卡号"

+ " 返回明细条数" + " " + "     交易地区号1"

+ "     交易币种1" + "" + " " + "     交易地区号3"

+ "     交易币种3" + "" + " " + "     交易地区号2"

+ "     交易币种2" + "" + "";

String fileName = "C:\\Users\\rysh101\\Desktop\\工作资料\\xxx.xml";

String readToString = readToString(fileName);

JSONObject jsonObject = xmltoJson(readToString);

System.out.println(jsonObject);

}

/**

*

* 功能描述:

* 〈功能详细描述〉将文本文件内容读取成字符串

*

* @param fileName 文件地址

* @return

*/

public static String readToString(String fileName) {

String encoding = "GBK";

File file = new File(fileName);

Long filelength = file.length();

byte[] filecontent = new byte[filelength.intValue()];

try {

FileInputStream in = new FileInputStream(file);

in.read(filecontent);

in.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

try {

return new String(filecontent, encoding);

} catch (UnsupportedEncodingException e) {

System.err.println("The OS does not support " + encoding);

e.printStackTrace();

return null;

}

}

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐