在實際的開發中,我們通常會設計到xml檔案的解析,其中dom4j就是乙個不錯的選擇.dom4j的效能和易用性方面都是屈指可數的.為了更加方便使用dom4j解析xml檔案,本人寫出了乙個xml的解析工具類, 從更大粒度上支援xml檔案的解析.該工具支援萬用字元號,應該算的上是乙個很方便和功能強健的類.
/*** author: crazy_rain
* date: 2007-2-14
* time: 上午09:45:17
* introduction:處理xml 檔案的工具類
*/public class xmlutil
/*** 將xml元素輸出到控制台
* * @param xml
*/public static void dump(node xml) catch (throwable t)
}/**
* 將document 物件寫入指定的檔案
* * @param xml
* @param filename
*/public static void dumptofile(node xml, string filename) catch (ioexception e)
}/**
* 獲得xml 文件物件
* * @param xmlfile
* 指向xml 檔案的引用
* @return xmldoc 從檔案讀取xml document
*/public static document read(file xmlfile) catch (documentexception e)
return document;
}/**
* 通過xml 檔案的名字讀取document物件
* * @param xmlfilename
* @return document
*/public static document read(string xmlfilename)
/*** 通過指向xml 檔案的url獲得document物件
* * @param url
* @return document
*/public static document read(url url) catch (documentexception e)
return document;
}/**
* 將xml node 物件轉換成 string
* * @param node
* @return string
*/public static string nodetostring(node node)
/*** 將字串解析成node
* * @param xmlstring
* @return node
*/public static node stringtonode(string xmlstring) catch (documentexception e)
return node;
}/**
* 根據給定路徑查詢給定 xml 元素的子元素
* * @param parent
* 父節點元素
* @param childpath
* 相對與父節點的子節點路徑,路徑組成部分之間用"/"分割,支援萬用字元號"*"
* @return child 子節點元素
*/public static element child(element parent, string childpath) else
if (child == null)
}return child;
}/**
* 根據給定路徑查詢給定 xml 元素的子元素
* * @param parent
* 父節點元素
* @param childpath
* 相對與父節點的子節點路徑,路徑組成部分之間用"/"分割,支援萬用字元號"*"
* @param index 子節點在兄弟列表中的位置
* @return child 子節點元素
*/public static element child(element parent, string childpath,int index) else
if (child == null)
}return child;
}/**
* 查詢父節點的子節點的屬性值
* * @param parent
* 父節點
* @param attributepath
* 子節點屬性相對于父節點的路徑,路徑各組成部分用"/"分割, 屬性名稱前要新增"@"符號
* 支援萬用字元號"*"
* @return 子節點的屬性值,如果找不到返回null
*/public static string childattribute(element parent, string attributepath)
return attributevalue;
}/**
* 根據相對于父節點的子節點路徑,查詢子節點列表
* * @param parent
* 父節點
* @param childrenpath
* 子節點路徑
* @return children 子節點列表
*/public static list children(element parent, string childrenpath) else
return children;
}/**
* 建立乙個xml 元素
* @param name xml 元素的名稱
* @param attributes 元素屬性
* @param ns 命名空間
* @return
*/public static element createelement(string name,mapattributes,namespace ns)else
for(string key: attributes.keyset())
return element;
}/**
* 建立xml 文件
* @param nsarray 命名空間陣列
* @param root
* @return
*/public static document createdocument(namespacensarray,element root)
if(nsarray != null && nsarray.length > 0)
}return document;}}
Xml程式設計 Dom4j解析工具
建立解析器 saxreader reader new saxreader 利用解析器讀入xml文件 document document reader.read new file input.xml 獲取文件的根節點 element root document.getrootelement 介面繼承結...
xml之DOM方式解析,DOM4J工具解析原理
dom解析原理 dom解析原理 xml解析器一次性把整個xml文件載入進記憶體,然後在記憶體中構建一顆document的物件樹,通過document物件,得到樹上的節點物件,通過節點物件訪問 操作 到xml文件的內容。一張圖來完全概括這個原理。而dom4j原理是dom方式解析的,下面介紹dom4j ...
xml檔案解析 DOM4J
讀取並解析xml文件 讀寫xml文件主要依賴於org.dom4j.io包,其中 提供domreader 和saxreader 兩類不同方式,而呼叫方式是一樣的。這就是依靠介面的好處。從檔案讀取 xml,輸入檔名,返回 xml文件 publicdocument read string filename...