Dom4jFactory(读取包含命名空间的XML(支持节点读取不完整))添加使用实例
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;
}
}
}
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 > 2 </ columns >
< 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 >
< 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 > 2 </ columns >
< 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 " );
DefaultElement columnsitem = df.getSelectObject( " columns " );
读取XML inputs节点下所有的input节点:
Dom4jFactory df
=
new
Dom4jFactory(
"
c:\\test.xml
"
);
List < DefaultElement > inputNodes = df.getSelectNodes( " input " );
List < DefaultElement > inputNodes = df.getSelectNodes( " input " );