import java.util.hashmap;
import java.util.list;
import java.util.map;
import org.dom4j.document;
import org.dom4j.documentexception;
import org.dom4j.element;
import org.dom4j.xpath;
import org.dom4j.io.saxreader;
/** *//**
* 读取有命名空间的xml(支持节点读取不完整)
* @author ai bo
* @version 1.3.1 date 2011-12-14
*/
public class dom4jfactory {
private saxreader reader = new saxreader();
private document document;
private static element root;
private static map<string, string> xmlmap = new hashmap<string, string>();
public dom4jfactory(string path) {
super();
try {
document = reader.read(path);
root = document.getrootelement();
string defaultnamespace = root.getnamespaceuri();
xmlmap.put("default", defaultnamespace);
} catch (documentexception e) {
e.printstacktrace();
}
}
public static list getselectnodes(string arg0) {
xpath selector = root.createxpath("//default:" arg0);
selector.setnamespaceuris(xmlmap);
return selector.selectnodes(root);
}
@suppresswarnings("unchecked")
public <t> t getselectobject(string arg0){
list selectnodes = getselectnodes(arg0);
if(selectnodes.size()==1){
return (t) selectnodes.get(0);
}else{
return null;
}
}
}
xml文件:
xml version="1.0" encoding="utf-8"?>
<inputs xmlns="http://input.abio.com.cn" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="http://input.abio.com.cn http://www.abio.com.cn/sproinput.xsd">
<columns>2columns>
<input id="id" datatype="string" minlen="1" maxlen="32" casetype="u" width="40" name_zh="数据代码" inputtype="text" editable="2" gridwidth="3">input>
<input id="organno" datatype="integer" nullable="false" minlen="1" maxlen="20" casetype="u" width="40" name_zh="机构编号" inputtype="text" editable="2">input>
<input id="organname" datatype="string" nullable="false" minlen="1" maxlen="20" casetype="u" width="40" name_zh="机构名称" inputtype="text" editable="2">input>
<input id="porganno" datatype="string" nullable="false" minlen="1" maxlen="20" casetype="u" width="40" name_zh="父机构编号" inputtype="text" editable="2">input>
<input id="efftime" datatype="date" validator="date" nullable="false" format="yyyy-mm-dd" minlen="8" maxlen="8" casetype="u" width="40" name_zh="有效日期" inputtype="date" editable="2">input>
<input id="createuser" datatype="string" minlen="1" maxlen="20" casetype="u" width="40" name_zh="创建人" inputtype="text" editable="2">input>
<input id="createtime" datatype="date" minlen="10" maxlen="20" casetype="u" width="40" name_zh="创建日期" editable="2" inputtype="text">input>
<input id="updateuser" datatype="string" minlen="1" maxlen="20" casetype="u" width="40" name_zh="更新人" inputtype="text" editable="2">input>
<input id="updatetime" datatype="date" minlen="10" maxlen="20" casetype="u" width="40" name_zh="更新日期" editable="2" inputtype="text">input>
inputs> 读取xml单个节点(inputs下的columns节点):
dom4jfactory df = new dom4jfactory("c:\\test.xml");
defaultelement columnsitem = df.getselectobject("columns"); 读取xml inputs节点下所有的input节点:
dom4jfactory df = new dom4jfactory("c:\\test.xml");
list<defaultelement> inputnodes = df.getselectnodes("input");
]]>