/**
* xml 工具类
*
* @ClassName: XmlUtil
* @author shenggm
* @date 2016-7-8 上午11:56:44
*
*/
public class XmlUtil {
/**
* 通过文件名创建documnet
*
* @Author: jianghan
* @param inputStream
* @return
*
*/
public static Document createDocument(InputStream inputStream) {
SAXReader reader = new SAXReader();
Document doc = null;
try {
doc = reader.read(inputStream);
} catch (DocumentException e) {
e.printStackTrace();
}
return doc;
}
/**
* 通过文件名创建根节点
*
* @Author: jianghan
* @param inputStream
* @return
*
*/
public static Element createRootElement(InputStream inputStream) {
return createDocument(inputStream).getRootElement();
}
/**
* 获取当前节点的名字,内容等属性
*
* @Author: jianghan
* @param node
* @return
*
*/
public static Map<String, String> getElement(Element node) {
Map<String, String> map = new HashMap<String, String>();
map.put("name", node.getName());
map.put("value", node.getText());
return map;
}
/**
* 获取当前节点的所有子节点
*
* @Author: jianghan
* @return
*
*/
@SuppressWarnings("unchecked")
public static List<Element> getChildElement(Element node) {
List<Element> elements = new ArrayList<Element>();
Iterator<Element> iterator = node.elementIterator();
while (iterator.hasNext()) {
Element e = iterator.next();
elements.add(e);
}
return elements;
}
public static List<Map<String, Object>> getAttributes(Element node) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
int i = 0;
for (Iterator<?> it = node.attributeIterator(); it.hasNext();) {
Attribute attribute = (Attribute) it.next();
list.get(i).put(attribute.getName(), attribute.getText());
i++;
}
return list;
}
}
package com.xj.hhjk.common.util;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
1173

被折叠的 条评论
为什么被折叠?



