使用SAXReader需要导入dom4j-full.jar包。
dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的。dom4j是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它。
使用举例:
1. s.xml内容
- <?xml version="1.0" encoding="GB2312"?>
- <data>
- <row queryDTO.enterpriseId="gfd" queryDTO.loginName="gdfg" queryDTO.state="0"/>
- </data>
2.解析
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.AbstractAttribute;
public class ReadXMLTest {
public static void main(String[] args){
File xmlFile = new File("C:/s.xml");
FileInputStream fis = null;
try {
fis = new FileInputStream(xmlFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.err.println("File is not exsit!");
}
SAXReader saxReader = new SAXReader();
List rowList = null;
try {
//生成文档对应实体
Document doc = saxReader.read(fis);
//获取指定路径下的元素列表,这里指获取所有的data下的row元素
rowList = doc.selectNodes("//data/row");
} catch (DocumentException e) {
e.printStackTrace();
}
for(Iterator iter = rowList.iterator();iter.hasNext();){
//获得具体的row元素
Element element = (Element)iter.next();
//获得row元素的所有属性列表
List elementList = element.attributes();
for(Iterator iter1 = elementList.iterator();iter1.hasNext();){
//将每个属性转化为一个抽象属性,然后获取其名字和值
AbstractAttribute aa = (AbstractAttribute)iter1.next();
System.out.println("Name:"+aa.getName()+";Value:"+aa.getValue());
}
//输出:
//Name:queryDTO.enterpriseId;Value:gfd
//Name:queryDTO.loginName;Value:gdfg
//Name:queryDTO.state;Value:0
System.out.println(element.getName());
//输出:
//row
// 取得row元素的queryDTO.enterpriseId属性的值
System.out.println(element.attributeValue("queryDTO.enterpriseId"));
//输出:
//gfd
//如果element下有子元素,(类似width="**"),要想获得该子元素的值,可以用如下方法
System.out.println(element.elementText("width"));//因为没有,所以输出为null。
}
}
}
当你解析XML时,是否会因为命名空间的存在而不能得偿所愿呢?
.net上的解决方法我就不多说了(.net有世界上最详细的开发文档。这是我最欣赏微软的地方)
java方面,好多人推荐用dom4j处理xml,我也就说说在dom4j上处理带命名空间的xml
先说前两个方法,是从网上看来的。(来自http://blog.csdn.net/anyoneking/)摘抄如下:
xml代码example:
<list-property name="cssStyleSheets">
<structure>
<property name="fileName">D: eport.css</property>
</structure>
</list-property>
</report>
第一个方案.设置你的xpath的命名空间setNamespaceURIs
public static void main(String[] args) throws Exception{
Map map = new HashMap();
map.put("design","http://www.eclipse.org/birt/2005/design" );
SAXReader saxReader = new SAXReader();
File file = new File("D:\test.xml" );
Document document = saxReader.read(file);
XPath x = document.createXPath("//design:list-property" );
x.setNamespaceURIs(map);
List nodelist = x.selectNodes(document);
System.out .println(nodelist.size());
}
}
第二个解决方案:设置你的DocumentFactory()的命名空间 setXPathNamespaceURIs
public static void main(String[] args) throws Exception{
Map map = new HashMap();
map.put("design","http://www.eclipse.org/birt/2005/design" );
SAXReader saxReader = new SAXReader();
File file = new File("D:\test.xml" );
saxReader.getDocumentFactory().setXPathNamespaceURIs(map);
Document document = saxReader.read(file);
List tmp = document.selectNodes("//design:list-property" );
System.out .println(tmp.size());
}
}
第三种方法:本人用的,最笨也是最通用的方法,就是不使用开发环境给你提供的一系列对象,而是用XPath语法中自带的local-name() 和 namespace-uri() 指定你要使用的节点名和命名空间。
当你遇到使用xslt来样式化xml时,就知道这个笨方法的好处了:
public static void main(String[] args) throws Exception
SAXReader saxReader = new SAXReader();
File file = new File("D:\test.xml" );
Document document = saxReader.read(file);
List tmp = document.selectNodes("//*[local-name()='report' and namespace-uri()='http://www.eclipse.org/birt/2005/design']/* [local-name()='list-property']" );
System.out .println(tmp.size());
}
}