xml转化为Map

方法:

public static Map<String, String> changeMap(String xmlDoc) {

if (!xmlDoc.equals("") && (xmlDoc != null)) {
// 创建一个新的字符串
StringReader xmlString = new StringReader(xmlDoc);
// 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
InputSource source = new InputSource(xmlString);
// 创建一个新的SAXBuilder
SAXBuilder saxb = new SAXBuilder();
// 定义返回对象
Map<String, String> reMap = new HashMap<String, String>();

try {
// 通过输入源构造一个Document
Document doc = saxb.build(source);
// 取的根元素
Element root = doc.getRootElement();

// 得到根元素所有子元素的集合
List node = root.getChildren();
Element et = null;

for (int i = 0; i < node.size(); i++) {
// 循环依次得到子元素
et = (Element) node.get(i);
// 得到内层子节点
List subNode = et.getChildren();
if (subNode.size() == 0) {
// 装入到Map中
reMap.put(et.getName(), et.getText());
} else {
Element subEt = null;
for (int j = 0; j < subNode.size(); j++) {
// 循环依次得到子元素
subEt = (Element) subNode.get(j);
// reMap.putAll(changeMap(reMap, subEt));
reMap = changeMap(reMap, subEt);
}
}

}
} catch (Exception e) {
log.error("\n返回XML解析失败," + e.getMessage());
}
return reMap;

} else {
return null;
}

}

/**
*
* @description 递归解析XML
* @param map
* @param et
* @return Map<String, String>
*/
@SuppressWarnings("unchecked")
public static Map<String, String> changeMap(Map<String, String> map,
Element et) {
// 定义返回对象
Map<String, String> reMap = new HashMap<String, String>();
try {
// 得到内层子节点
List subNode = et.getChildren();
reMap.putAll(map);
if (subNode.size() == 0) {

// 装入到Map中
if (reMap.containsKey(et.getName())) {
String key = "";
for (int i = 0; i < 100 * 10; i++) {
key = et.getName() + i;
if (reMap.containsKey(key)) {
continue;
} else {
reMap.put(key, et.getText());
break;
}
}
} else {
reMap.put(et.getName(), et.getText());
}

} else {
Element subEt = null;
for (int i = 0; i < subNode.size(); i++) {
// 循环依次得到子元素
subEt = (Element) subNode.get(i);
reMap = changeMap(reMap, subEt);
}
}
} catch (Exception e) {
log.error("\n返回XML解析失败," + e.getMessage());
}
return reMap;
}

时间: 2024-08-24 21:08:32

xml转化为Map的相关文章

xml中所有元素转化为map

package demo; import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; import java.util.Iterator; import java.util.Map; /* *dom4j */ import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelp

Android中序列化对象到XMl 和 XML反序列化为对象

package com.example.xmloperation; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import java.util.Random; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; imp

利用反射机制 实现 javabean 转化为 map

package com.test.entity; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.Collection; import java.util.Date; import java.util.HashMap;

XML字符串转为Map集合

public class xmlToMapUtils { /** * xml字符串转为map集合 * @param xmlStr * @return */ public static Map<String, Object> xmlToMap(String xmlStr){ List<Map<String, String>> resultList = iterateWholeXML(xmlStr); Map<String, Object> retMap = n

java XML解析成Map

1.需要解析的文件.xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <request> 3 <realName>test</realName> 4 <identityNumber>411525152242417185276</identityNumber> 5 <phone>1314456788</phone> 6 <u

简单json语句转化为map保存

主要用到了 net.sf.json.JSONObject类 需要用到的jar包 : jar包下载地址 1 package test; 2 3 import java.io.BufferedReader; 4 import java.io.InputStream; 5 import java.io.InputStreamReader; 6 import java.net.HttpURLConnection; 7 import java.net.URL; 8 import java.util.Has

Java xml文件转换为map集合

public class XmlToMap {  public static Map<String, Object> Dom2Map(Document doc){           Map<String, Object> map = new HashMap<String, Object>();           if(doc == null)               return map;           Element root = doc.getRoot

bean转化为xml 与 xml转化为bean

1.bean to xml: public static void main(String[] args) { List<JydInfo> list = new ArrayList<JydInfo>(); String auth_code = "hunan_auth_code"; JydInfo clws1 = new JydInfo("100101","黑猪","100","2000&quo

List转化为Map

List<JfAuction> jfAuctionListValue=new ArrayList<JfAuction>();                 Map<Integer, JfAuction> jfAuctionMap = new HashMap<Integer, JfAuction>();                   for (Integer key : jfAuctionMap.keySet()) {