在项目中,因为需要解析xml,因此做了一部分工作,写了个工具类,以备以后使用:
XmlInfo.java:
/**/
/*
* TreePrinter.java
* 创建日期 2007-4-23
* @author: liaoyuan
*
*/
package
com.sac.common;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
java.io.IOException;
import
java.io.StringReader;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.Iterator;
import
java.util.List;
import
java.util.Map;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
org.apache.commons.lang.StringUtils;
import
org.jdom.Document;
import
org.jdom.Element;
import
org.jdom.JDOMException;
import
org.jdom.input.SAXBuilder;
import
org.jdom.output.XMLOutputter;
import
org.xml.sax.InputSource;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/** */
/**
*
* XmlInfo
* @author liaoyuan
* @mailto:yuan.liao@broha.com
* 2007-4-24 10:43:33
*
*/
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
XmlInfo
...
{
private static final String ENCODING = "gb2312";
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static ArrayList anaylzXml(Document document, String nodeName) ...{
ArrayList arrylist = new ArrayList();
// List list = document.getRootElement().getChild(nodeName).getChildren();
List list = document.getRootElement().getChildren();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (int i = 0; i < list.size(); i++) ...{
Map map = new HashMap();
Element child = (Element) list.get(i);
inspect2(child, map);// 调用
arrylist.add(map);
}
return arrylist;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void inspect2(Element element, Map map) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (!element.isRootElement()) ...{
// System.out.println();
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (!element.hasChildren()) ...{
String qualifiedName = element.getName();
map.put(qualifiedName, element.getText());
// System.out.println(qualifiedName + ":" + element.getText());
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} else ...{
List list = element.getContent();
Iterator iterator = list.iterator();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
while (iterator.hasNext()) ...{
Object o = iterator.next();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (o instanceof Element) ...{// 如果是子元素
Element child = (Element) o;
inspect2(child, map);// 递归调用
}
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//jdom String 转换成document
public static Document String2Doc(String content) throws IOException,
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
JDOMException ...{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//创建输入源
InputSource input = new org.xml.sax.InputSource(new StringReader(content));
//创建DOM解析器
SAXBuilder parser = new SAXBuilder();
//解析文档
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Document document = parser.build(input);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//返回文档
return document;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* 将文档对象输出为字符串
*
* @param document
* 文档对象
* @return
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static String toXMLString(Document document) ...{
return toXMLString(document, true);
}
//jdom document 转换成 String
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static String toXMLString(Document document, boolean bFormated) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try ...{
XMLOutputter outputformat = new XMLOutputter(" ", bFormated,
ENCODING);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return outputformat.outputString(document);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (Exception exception) ...{
return null;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* 用于调用OA查询接口,返回所需字段值,封装于map
* 默认取第一条值
* @param content
* @return
* @throws Exception
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static Map returnMap(String content) throws Exception ...{
Map map = null;
ArrayList list = null;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Document document = String2Doc(content);
// System.out.println("****** " + toXMLString(document,false));
list = anaylzXml(document, "ReturnData");
if(list!=null)
map=(Map)list.get(0);
System.out.println("list=== " + list);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return map;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/** *//**
* 用于调用OA查询接口,返回多条记录 所需字段值,封装于ArrayList
* 默认取第一条值
* @param content
* @return
* @throws Exception
*/
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static ArrayList returnList(String content) throws Exception ...{
ArrayList list = null;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Document document = String2Doc(content);
list = anaylzXml(document, "ReturnData");
System.out.println("list=== " + list);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return list;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static Document loadDoc(String url) throws JDOMException ...{
if (StringUtils.isEmpty(url))
url = "D:/task/jars/test1.xml";
SAXBuilder parser = new SAXBuilder();
Document document = parser.build(url);
return document;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main(String[] args) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try ...{
String content = toXMLString(loadDoc(null), false);
Map map = returnMap(content);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (Exception e) ...{
e.printStackTrace();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
inspect2 此方法循环调用,遍历所有子节点元素.
//
解析XML,content 字符串流,tablename 要循环遍历的节点
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
static
Vector opStringXml(String content,String tablename)
throws
Exception
...
{
Vector vect=null;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(StringUtils.isEmpty(content))...{
throw new Exception("输入内容为空!");
}
Document doc=XMLHelper.String2Doc(content);
if(doc==null) return null;
Element root = doc.getDocumentElement(); //根元素
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
vect=new Vector();
NodeList rootname = root.getElementsByTagName(tablename);
// NodeList rootname = root.getChildNodes();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (rootname != null) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (int i = 0; i < rootname.getLength(); i++) ...{//循环遍历数据节点
Map propertyCache=new HashMap();
Element simple = (Element) rootname.item(i);
NodeList aa = simple.getChildNodes();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for(int j=0;j<aa.getLength();j++)...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try...{
Node n = (Node)aa.item(j);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(n !=null)...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(!n.getNodeName().equals("#text"))...{
// System.out.println("j="+j);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if(n.getFirstChild()!=null)...{
propertyCache.put(n.getNodeName().trim(),n.getFirstChild().getNodeValue().trim());
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
}catch(Exception e)...{
e.printStackTrace();
}
}
vect.add(propertyCache);
}
}
return vect;
}