java使用dom4j读取xml的例子
?dom4j下載地址:http://sourceforge.net/projects/dom4j/files/dom4j/1.6.1/dom4j-1.6.1.tar.gz/download
例如讀取如下xml文件:
8G200158010G5003000dom4j讀取上面xml文件的代碼如下:
public class Simple {
public static void main(String[] args) throws DocumentException {
SAXReader reader = new SAXReader();
Document doc = reader.read(Simple.class.getClassLoader()
.getResourceAsStream("test.xml")); // 構(gòu)造文檔對(duì)象
Element root = doc.getRootElement(); // 獲取根元素
Element foo;
for (Iterator i = root.elementIterator("disk"); i.hasNext();) {
foo = (Element) i.next();
System.out.println("磁盤信息:");
System.out.println("分區(qū)盤符:" + foo.attributeValue("name"));
System.out.println("分區(qū)容量:" + foo.elementText("capacity"));
System.out.println("目錄數(shù):" + foo.elementText("directories"));
System.out.println("文件數(shù):" + foo.elementText("files"));
System.out.println("-----------------------------------");
}
}
}
轉(zhuǎn)載于:https://blog.51cto.com/shuaigee/1133140
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的java使用dom4j读取xml的例子的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: NAnt简单介绍和我第一个例子
- 下一篇: 导函数的介质定理