在项目中,因为需要解析xml,因此做了一部分工作,写了个工具类,以备以后使用:
XmlInfo.java:

/**//*
* TreePrinter.java
* 创建日期 2007-4-23
* @author: liaoyuan
*
*/
package com.sac.common;

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;

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;


/** *//**
*
* XmlInfo
* @author liaoyuan
* @mailto:yuan.liao@broha.com
* 2007-4-24 10:43:33
*
*/

public class XmlInfo ...{
private static final String ENCODING = "gb2312";


public static ArrayList anaylzXml(Document document, String nodeName) ...{
ArrayList arrylist = new ArrayList();
// List list = document.getRootElement().getChild(nodeName).getChildren();
List list = document.getRootElement().getChildren();


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;
}


public static void inspect2(Element element, Map map) ...{

if (!element.isRootElement()) ...{
// System.out.println();
}

if (!element.hasChildren()) ...{
String qualifiedName = element.getName();
map.put(qualifiedName, element.getText());
// System.out.println(qualifiedName + ":" + element.getText());

} else ...{
List list = element.getContent();
Iterator iterator = list.iterator();

while (iterator.hasNext()) ...{
Object o = iterator.next();

if (o instanceof Element) ...{// 如果是子元素
Element child = (Element) o;
inspect2(child, map);// 递归调用
}
}
}
}

//jdom String 转换成document
public static Document String2Doc(String content) throws IOException,

JDOMException ...{

//创建输入源
InputSource input = new org.xml.sax.InputSource(new StringReader(content));
//创建DOM解析器
SAXBuilder parser = new SAXBuilder();
//解析文档

Document document = parser.build(input);

//返回文档
return document;
}

/** *//**
* 将文档对象输出为字符串
*
* @param document
* 文档对象
* @return
*/

public static String toXMLString(Document document) ...{
return toXMLString(document, true);
}
//jdom document 转换成 String

public static String toXMLString(Document document, boolean bFormated) ...{

try ...{
XMLOutputter outputformat = new XMLOutputter(" ", bFormated,
ENCODING);

return outputformat.outputString(document);

} catch (Exception exception) ...{
return null;
}
}


/** *//**
* 用于调用OA查询接口,返回所需字段值,封装于map
* 默认取第一条值
* @param content
* @return
* @throws Exception
*/

public static Map returnMap(String content) throws Exception ...{
Map map = null;
ArrayList list = null;

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);

return map;
}


/** *//**
* 用于调用OA查询接口,返回多条记录 所需字段值,封装于ArrayList
* 默认取第一条值
* @param content
* @return
* @throws Exception
*/

public static ArrayList returnList(String content) throws Exception ...{
ArrayList list = null;

Document document = String2Doc(content);
list = anaylzXml(document, "ReturnData");
System.out.println("list=== " + list);

return list;
}

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;
}


public static void main(String[] args) ...{


try ...{
String content = toXMLString(loadDoc(null), false);
Map map = returnMap(content);

} catch (Exception e) ...{
e.printStackTrace();
}

}

}
inspect2 此方法循环调用,遍历所有子节点元素.
//解析XML,content 字符串流,tablename 要循环遍历的节点

public static Vector opStringXml(String content,String tablename) throws Exception ...{
Vector vect=null;


if(StringUtils.isEmpty(content))...{
throw new Exception("输入内容为空!");
}
Document doc=XMLHelper.String2Doc(content);
if(doc==null) return null;
Element root = doc.getDocumentElement(); //根元素

vect=new Vector();
NodeList rootname = root.getElementsByTagName(tablename);
// NodeList rootname = root.getChildNodes();

if (rootname != null) ...{

for (int i = 0; i < rootname.getLength(); i++) ...{//循环遍历数据节点
Map propertyCache=new HashMap();
Element simple = (Element) rootname.item(i);
NodeList aa = simple.getChildNodes();

for(int j=0;j<aa.getLength();j++)...{

try...{
Node n = (Node)aa.item(j);

if(n !=null)...{

if(!n.getNodeName().equals("#text"))...{
// System.out.println("j="+j);

if(n.getFirstChild()!=null)...{
propertyCache.put(n.getNodeName().trim(),n.getFirstChild().getNodeValue().trim());
}
}
}

}catch(Exception e)...{
e.printStackTrace();
}
}
vect.add(propertyCache);
}
}
return vect;
}