<?xml version= encoding=GB standalone=no?><學生花名冊>
<學生 性別=男>
<姓名>李華</姓名>
<年齡></年齡>
</學生>
<學生 性別=男>
<姓名>張三</姓名>
<年齡></年齡>
</學生>
<學生 性別=女>
<姓名>王娟</姓名>
<年齡></年齡>
</學生>
</學生花名冊>
<?xml version= encoding=GB standalone=no?><學生花名冊>
<學生 性別=男>
<姓名>李華</姓名>
<年齡></年齡>
</學生>
<學生 性別=男>
<姓名>張三</姓名>
<年齡></年齡>
</學生>
<學生 性別=女>
<姓名>王娟</姓名>
<年齡></年齡>
</學生>
</學生花名冊>
數據模型Studentjava
[java] view plaincopyprint?
package comupcgriddom;
public class Student {
private String name;
private String sex;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
thissex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
thisage = age;
}
}
package com
upcgrid
dom;
public class Student {
private String name;
private String sex;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this
name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this
sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this
age = age;
}
}
DOM方式解析xml文檔示例程序
[java] view plaincopyprint?
package com
upcgrid
dom;
import java
io
FileOutputStream;
import java
io
IOException;
import java
io
OutputStreamWriter;
import java
util
Vector;
import javax
xml
parsers
DocumentBuilder;
import javax
xml
parsers
DocumentBuilderFactory;
import javax
xml
parsers
ParserConfigurationException;
import javax
xml
transform
OutputKeys;
import javax
xml
transform
Transformer;
import javax
xml
transform
TransformerException;
import javax
xml
transform
TransformerFactory;
import javax
xml
transform
dom
DOMSource;
import javax
xml
transform
stream
StreamResult;
import org
w
c
dom
Document;
import org
w
c
dom
Element;
import org
w
c
dom
NodeList;
import org
w
c
dom
Text;
import org
xml
sax
SAXException;
/**
*
* @author shijin 原始DOM解析XML方式
*
*/
public class DOMTest {
public static void main(String[] args) throws ParserConfigurationException
SAXException
IOException
TransformerException {
DOMTest test = new DOMTest()
test
deleteElement(
name
xml
女
)
Vector<Student> stus = test
changeXMLToModel(
name
xml
)
for (Student stu : stus) {
System
out
print(stu
getName() +
+ stu
getSex() +
+ stu
getAge() +
\n
)
}
// 輸出
// 李華 男
// 張三 男
test
createDocument(
name
xml
stus)
}
/**
* show 根據vector中的student對象創建xml文檔
*
* @param filename
* 要創建的文檔名
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
* @throws TransformerException
*/
public void createDocument(String filename
Vector<Student> stus)
throws ParserConfigurationException
SAXException
IOException
TransformerException {
// 得到DOM解析器的工廠實例
DocumentBuilderFactory dbf = DocumentBuilderFactory
newInstance()
// 從DOM工廠獲取DOM解析器
DocumentBuilder builder = dbf
newDocumentBuilder()
// 創建document對象
Document doc = builder
newDocument()
Element root = doc
createElement(
學生花名冊
)
// 創建根元素
doc
appendChild(root)
// 添加根元素
for (Student stu : stus) {
Element stuElement = doc
createElement(
學生
)
stuElement
setAttribute(
性別
stu
getSex())
Element nameElement = doc
createElement(
姓名
)
Text nameText = doc
createTextNode(stu
getName())
nameElement
appendChild(nameText)
Element ageElement = doc
createElement(
年齡
)
Text ageText = doc
createTextNode(String
valueOf(stu
getAge()))
ageElement
appendChild(ageText)
stuElement
appendChild(nameElement)
stuElement
appendChild(ageElement)
root
appendChild(stuElement)
}
FileOutputStream fos = new FileOutputStream(filename)
OutputStreamWriter ow = new OutputStreamWriter(fos)
// 保存xml文件
DOMSource doms = new DOMSource(doc)
// 創建結果輸出流
StreamResult result = new StreamResult(ow)
// 得到轉換工廠實例
TransformerFactory transFac = TransformerFactory
newInstance()
// 得到轉換器實例
Transformer trans = transFac
newTransformer()
// 轉化器設置輸出文檔的編碼方式
trans
setOutputProperty(OutputKeys
ENCODING
GB
)
//設置支持縮進
trans
setOutputProperty(OutputKeys
INDENT
yes
)
// 將文檔源轉換到結果輸出流
trans
transform(doms
result)
}
/**
* show 讀取指定XML文檔並將其對象化
*
* @param filename
* @return 存儲Student對象的vector
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
public Vector<Student> changeXMLToModel(String filename)
throws ParserConfigurationException
SAXException
IOException {
// 得到DOM解析器的工廠實例
DocumentBuilderFactory dbf = DocumentBuilderFactory
newInstance()
// 從DOM工廠獲取DOM解析器
DocumentBuilder builder = dbf
newDocumentBuilder()
// 解析xml文檔
獲得document對象
即DOM樹
Document doc = builder
parse(filename)
// 獲取根節點
Element root = doc
getDocumentElement()
// 獲取根節點的子節點中名字為
學生
的節點列表
NodeList stuNodes = root
getElementsByTagName(
學生
)
Vector<Student> students = new Vector<Student>()
// 遍歷<學生>節點
for (int i =
; i < stuNodes
getLength()
i++) {
// 獲取一個學生節點
Element stuNode = (Element) em(i)
// 創建一個學生對象
Student stu = new Student()
stu
setSex(stuNode
getAttribute(
性別
))
// 獲取<學生>節點的子節點中名字為
姓名
的節點列表
NodeList nameNodes = stuNode
getElementsByTagName(
姓名
)
// 取得第一個姓名
Element nameNode = (Element) em(
)
stu
setName(nameNode
getTextContent())
// 獲取<學生>節點的子節點中名字為
年齡
的節點列表
NodeList ageNodes = stuNode
getElementsByTagName(
年齡
)
// 取得第一個年齡
Element ageNode = (Element) em(
)
stu
setAge(Integer
parseInt(ageNode
getTextContent()))
students
add(stu)
}
return students;
}
/**
* show 刪除指定名為filename的xml文檔中學生結點的性別屬性為tagname的結點
*
* @param filename
* @param tagname
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
* @throws TransformerException
*/
public void deleteElement(String filename
String tagname)
throws ParserConfigurationException
SAXException
IOException
TransformerException {
// 得到DOM解析器的工廠實例
DocumentBuilderFactory dbf = DocumentBuilderFactory
newInstance()
// 從DOM工廠獲取DOM解析器
DocumentBuilder builder = dbf
newDocumentBuilder()
// 解析xml文檔
獲得document對象
即DOM樹
Document doc = builder
parse(filename)
// 找到刪除節點
NodeList stuNodes = doc
getElementsByTagName(
學生
)
for (int i =
; i < stuNodes
getLength()
i++) {
Element stuElement = (Element) em(i)
String sex = stuElement
getAttribute(
性別
)
if (sex
equals(tagname)) {
stuElement
getParentNode()
removeChild(stuElement)
}
}
// 保存xml文件
DOMSource doms = new DOMSource(doc)
// 創建結果輸出流
StreamResult result = new StreamResult(new FileOutputStream(filename))
// 得到轉換工廠實例
TransformerFactory transFac = TransformerFactory
newInstance()
// 得到轉換器實例
Transformer trans = transFac
newTransformer()
// 轉化器設置輸出文檔的編碼方式
trans
setOutputProperty(OutputKeys
ENCODING
GB
)
// 將文檔源轉換到結果輸出流
trans
transform(doms
result)
}
}
package com
upcgrid
dom;
import java
io
FileOutputStream;
import java
io
IOException;
import java
io
OutputStreamWriter;
import java
util
Vector;
import javax
xml
parsers
DocumentBuilder;
import javax
xml
parsers
DocumentBuilderFactory;
import javax
xml
parsers
ParserConfigurationException;
import javax
xml
transform
OutputKeys;
import javax
xml
transform
Transformer;
import javax
xml
transform
TransformerException;
import javax
xml
transform
TransformerFactory;
import javax
xml
transform
dom
DOMSource;
import javax
xml
transform
stream
StreamResult;
import org
w
c
dom
Document;
import org
w
c
dom
Element;
import org
w
c
dom
NodeList;
import org
w
c
dom
Text;
import org
xml
sax
SAXException;
/**
*
* @author shijin 原始DOM解析XML方式
*
*/
public class DOMTest {
public static void main(String[] args) throws ParserConfigurationException
SAXException
IOException
TransformerException {
DOMTest test = new DOMTest()
test
deleteElement(
name
xml
女
)
Vector<Student> stus = test
changeXMLToModel(
name
xml
)
for (Student stu : stus) {
System
out
print(stu
getName() +
+ stu
getSex() +
+ stu
getAge() +
\n
)
}
// 輸出
// 李華 男
// 張三 男
test
createDocument(
name
xml
stus)
}
/**
* show 根據vector中的student對象創建xml文檔
*
* @param filename
* 要創建的文檔名
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
* @throws TransformerException
*/
public void createDocument(String filename
Vector<Student> stus)
throws ParserConfigurationException
SAXException
IOException
TransformerException {
// 得到DOM解析器的工廠實例
DocumentBuilderFactory dbf = DocumentBuilderFactory
newInstance()
// 從DOM工廠獲取DOM解析器
DocumentBuilder builder = dbf
newDocumentBuilder()
// 創建document對象
Document doc = builder
newDocument()
Element root = doc
createElement(
學生花名冊
)
// 創建根元素
doc
appendChild(root)
// 添加根元素
for (Student stu : stus) {
Element stuElement = doc
createElement(
學生
)
stuElement
setAttribute(
性別
stu
getSex())
Element nameElement = doc
createElement(
姓名
)
Text nameText = doc
createTextNode(stu
getName())
nameElement
appendChild(nameText)
Element ageElement = doc
createElement(
年齡
)
Text ageText = doc
createTextNode(String
valueOf(stu
getAge()))
ageElement
appendChild(ageText)
stuElement
appendChild(nameElement)
stuElement
appendChild(ageElement)
root
appendChild(stuElement)
}
FileOutputStream fos = new FileOutputStream(filename)
OutputStreamWriter ow = new OutputStreamWriter(fos)
// 保存xml文件
DOMSource doms = new DOMSource(doc)
// 創建結果輸出流
StreamResult result = new StreamResult(ow)
// 得到轉換工廠實例
TransformerFactory transFac = TransformerFactory
newInstance()
// 得到轉換器實例
Transformer trans = transFac
newTransformer()
// 轉化器設置輸出文檔的編碼方式
trans
setOutputProperty(OutputKeys
ENCODING
GB
)
//設置支持縮進
trans
setOutputProperty(OutputKeys
INDENT
yes
)
// 將文檔源轉換到結果輸出流
trans
transform(doms
result)
}
/**
* show 讀取指定XML文檔並將其對象化
*
* @param filename
* @return 存儲Student對象的vector
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
public Vector<Student> changeXMLToModel(String filename)
throws ParserConfigurationException
SAXException
IOException {
// 得到DOM解析器的工廠實例
DocumentBuilderFactory dbf = DocumentBuilderFactory
newInstance()
// 從DOM工廠獲取DOM解析器
DocumentBuilder builder = dbf
newDocumentBuilder()
// 解析xml文檔
獲得document對象
即DOM樹
Document doc = builder
parse(filename)
// 獲取根節點
Element root = doc
getDocumentElement()
// 獲取根節點的子節點中名字為
學生
的節點列表
NodeList stuNodes = root
getElementsByTagName(
學生
)
Vector<Student> students = new Vector<Student>()
// 遍歷<學生>節點
for (int i =
; i < stuNodes
getLength()
i++) {
// 獲取一個學生節點
Element stuNode = (Element) em(i)
// 創建一個學生對象
Student stu = new Student()
stu
setSex(stuNode
getAttribute(
性別
))
// 獲取<學生>節點的子節點中名字為
姓名
的節點列表
NodeList nameNodes = stuNode
getElementsByTagName(
姓名
)
// 取得第一個姓名
Element nameNode = (Element) em(
)
stu
setName(nameNode
getTextContent())
// 獲取<學生>節點的子節點中名字為
年齡
的節點列表
NodeList ageNodes = stuNode
getElementsByTagName(
年齡
)
// 取得第一個年齡
Element ageNode = (Element) em(
)
stu
setAge(Integer
parseInt(ageNode
getTextContent()))
students
add(stu)
}
return students;
}
/**
* show 刪除指定名為filename的xml文檔中學生結點的性別屬性為tagname的結點
*
* @param filename
* @param tagname
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
* @throws TransformerException
*/
public void deleteElement(String filename
String tagname)
throws ParserConfigurationException
SAXException
IOException
TransformerException {
// 得到DOM解析器的工廠實例
DocumentBuilderFactory dbf = DocumentBuilderFactory
newInstance()
// 從DOM工廠獲取DOM解析器
DocumentBuilder builder = dbf
newDocumentBuilder()
// 解析xml文檔
獲得document對象
即DOM樹
Document doc = builder
parse(filename)
// 找到刪除節點
NodeList stuNodes = doc
getElementsByTagName(
學生
)
for (int i =
; i < stuNodes
getLength()
i++) {
Element stuElement = (Element) em(i)
String sex = stuElement
getAttribute(
性別
)
if (sex
equals(tagname)) {
stuElement
getParentNode()
removeChild(stuElement)
}
}
// 保存xml文件
DOMSource doms = new DOMSource(doc)
// 創建結果輸出流
StreamResult result = new StreamResult(new FileOutputStream(filename))
// 得到轉換工廠實例
TransformerFactory transFac = TransformerFactory
newInstance()
// 得到轉換器實例
Transformer trans = transFac
newTransformer()
// 轉化器設置輸出文檔的編碼方式
trans
setOutputProperty(OutputKeys
ENCODING
GB
)
// 將文檔源轉換到結果輸出流
trans
transform(doms
result)
}
}
輸出
李華 男
張三 男
結果name
xml
[java] view plaincopyprint?
<?xml version=
encoding=
GB
standalone=
no
?><學生花名冊>
<學生 性別=
男
>
<姓名>李華</姓名>
<年齡>
</年齡>
</學生>
<學生 性別=
男
>
<姓名>張三</姓名>
<年齡>
</年齡>
</學生>
</學生花名冊>
<?xml version=
encoding=
GB
standalone=
no
?><學生花名冊>
<學生 性別=
男
>
<姓名>李華</姓名>
<年齡>
</年齡>
</學生>
<學生 性別=
男
>
<姓名>張三</姓名>
<年齡>
</年齡>
</學生>
</學生花名冊>
name
xml
[java] view plaincopyprint?
<?xml version=
encoding=
GB
standalone=
no
?>
<學生花名冊>
<學生 性別=
男
>
<姓名>李華</姓名>
<年齡>
</年齡>
</學生>
<學生 性別=
男
>
<姓名>張三</姓名>
<年齡>
</年齡>
</學生>
</學生花名冊>
<?xml version=
encoding=
GB
standalone=
no
?>
<學生花名冊>
<學生 性別=
男
>
<姓名>李華</姓名>
<年齡>
</年齡>
</學生>
<學生 性別=
男
>
<姓名>張三</姓名>
<年齡>
</年齡>
</學生>
</學生花名冊>
二
JDOM方式
HD
xml
[html] view plaincopyprint?
<?xml version=
encoding=
UTF
?>
<HD>
<disk name=
C
>
<capacity>
G</capacity>
<directories>
</directories>
<files>
</files>
</disk>
<disk name=
D
>
<capacity>
G</capacity>
<directories>
</directories>
<files>
</files>
</disk>
</HD>
<?xml version=
encoding=
UTF
?>
<HD>
<disk name=
C
>
<capacity>
G</capacity>
<directories>
</directories>
<files>
</files>
</disk>
<disk name=
D
>
<capacity>
G</capacity>
<directories>
</directories>
<files>
</files>
</disk>
</HD>
JDOM解析XML文檔示例
[html] view plaincopyprint?
package com
upcgrid
jdom;
import java
io
IOException;
import java
util
List;
import org
jdom
Document;
import org
jdom
Element;
import org
jdom
JDOMException;
import org
jdom
input
SAXBuilder;
public class JDOMTest {
public static void main(String[] args) throws JDOMException
IOException {
// 獲得JDOM以SAX方式處理XML文檔的構造器
SAXBuilder saxb = new SAXBuilder()
// 通過SAX構造器獲得XML文檔對象
Document doc = saxb
build(JDOMTest
class
getClassLoader()
getResourceAsStream(
test
xml
))
// 獲取根元素HD
Element root = doc
getRootElement()
// 取名字為disk的所有元素
List<Element> list = root
getChildren(
disk
)
for (int i =
; i < list
size()
i++) {
//取得第i個disk節點
Element diskElement = (Element) list
get(i)
//取得disk屬性name
String name = diskElement
getAttributeValue(
name
)
// 取disk子元素capacity的內容
String capacity = diskElement
getChildText(
capacity
)
String directories = diskElement
getChildText(
directories
)
String files = diskElement
getChildText(
files
)
System
out
println(
磁盤信息
)
System
out
println(
分區盤符
+ name)
System
out
println(
分區容量
+ capacity)
System
out
println(
目錄數
+ directories)
System
out
println(
文件數
+ files)
System
out
println(
)
}
}
}
package com
upcgrid
jdom;
import java
io
IOException;
import java
util
List;
import org
jdom
Document;
import org
jdom
Element;
import org
jdom
JDOMException;
import org
jdom
input
SAXBuilder;
public class JDOMTest {
public static void main(String[] args) throws JDOMException
IOException {
// 獲得JDOM以SAX方式處理XML文檔的構造器
SAXBuilder saxb = new SAXBuilder()
// 通過SAX構造器獲得XML文檔對象
Document doc = saxb
build(JDOMTest
class
getClassLoader()
getResourceAsStream(
test
xml
))
// 獲取根元素HD
Element root = doc
getRootElement()
// 取名字為disk的所有元素
List<Element> list = root
getChildren(
disk
)
for (int i =
; i < list
size()
i++) {
//取得第i個disk節點
Element diskElement = (Element) list
get(i)
//取得disk屬性name
String name = diskElement
getAttributeValue(
name
)
// 取disk子元素capacity的內容
String capacity = diskElement
getChildText(
capacity
)
String directories = diskElement
getChildText(
directories
)
String files = diskElement
getChildText(
files
)
System
out
println(
磁盤信息
)
System
out
println(
分區盤符
+ name)
System
out
println(
分區容量
+ capacity)
System
out
println(
目錄數
+ directories)
System
out
println(
文件數
+ files)
System
out
println(
)
}
}
}
輸出結果
磁盤信息
分區盤符
C
分區容量
G
目錄數
文件數
磁盤信息
分區盤符
D
分區容量
G
目錄數
文件數
三
DOM
J方式
User
hbm
xml
[html] view plaincopyprint?
<?xml version=
encoding=
UTF
?>
<hibernate
mapping>
<class name=
com
upcgrid
User
table=
t_user
>
<property name=
username
>aaa</property>
<property name=
password
>bbb</property>
</class》
</hibernate
mapping>
<?xml version=
encoding=
UTF
?>
<hibernate
mapping>
<class name=
com
upcgrid
User
table=
t_user
>
<property name=
username
>aaa</property>
<property name=
password
>bbb</property>
</class》
</hibernate
mapping>
DOM
J解析XML示例
[java] view plaincopyprint?
package com
upcgrid
dom
j;
import java
io
File;
import java
io
FileWriter;
import java
io
IOException;
import java
util
Iterator;
import java
util
List;
import org
dom
j
Attribute;
import org
dom
j
Document;
import org
dom
j
DocumentException;
import org
dom
j
DocumentHelper;
import org
dom
j
Element;
import org
dom
j
Node;
import org
dom
j
io
OutputFormat;
import org
dom
j
io
SAXReader;
import org
dom
j
io
XMLWriter;
public class DOM
JTest {
public static void main(String[] args) {
//獲得DOM
J以SAX方式處理XML文檔的管道
SAXReader reader = new SAXReader()
try {
//通過管道獲得XML文檔對象
Document document = reader
read(new File(
User
hbm
xml
))
//獲取根節點
Element root = document
getRootElement()
System
out
println(root
getName())
Element element = null;
//遍歷根節點下的節點
for(Iterator<Element> i = root
elementIterator()
i
hasNext()
){
//根節點的子節點
element = (Element) i
next()
System
out
println(element
getName())
//遍歷節點屬性
for(Iterator<Attribute> j = element
attributeIterator()
j
hasNext()
){
Attribute attr = (Attribute)j
next()
System
out
println(attr
getName()+
:
+attr
getValue())
}
//遍歷名為
property
的節點
for(Iterator<Element> k = element
elementIterator(
property
)
k
hasNext()
){
Element e
= (Element)k
next()
System
out
println(e
getData())
}
}
//通過Xpath的方式尋找節點
List<Node> list = document
selectNodes(
//hibernate
mapping/class/property
)
for(Node n: list){
String name = n
valueOf(
@name
)
System
out
println(n
getName()+
@name:
+name)
}
//只拿第一個
Node node = document
selectSingleNode(
//hibernate
mapping/class
)
String name = node
valueOf(
@name
)
String table = node
valueOf(
@table
)
System
out
println(node
getName()+
@name:
+name+
@table:
+table)
createDocument()
} catch (DocumentException e) {
e
printStackTrace()
}
}
public static Document createDocument(){
//創建document對象
Document document = DocumentHelper
createDocument()
Element root = document
addElement(
hibernate
mapping
)
//鏈式編程
Element classElement = root
addElement(
class
)
addAttribute(
name
com
upcgrid
User
)
addAttribute(
table
t_user
)
classElement
addElement(
property
)
addAttribute(
name
username
)
addText(
aaa
)
classElement
addElement(
Property
)
addAttribute(
name
password
)
addText(
bbb
)
try {
FileWriter out = new FileWriter(
User
hbm
xml
)
document
write(out)
out
flush()
out
close()
} catch (IOException e) {
e
printStackTrace()
}
try {
//設定美化格式
OutputFormat format = OutputFormat
createPrettyPrint()
XMLWriter writer = new XMLWriter(new FileWriter(
User
hbm
xml
)
format)
writer
write(document)
writer
close()
//縮減格式
} catch (IOException e) {
e
printStackTrace()
}
return document;
}
}
package com
upcgrid
dom
j;
import java
io
File;
import java
io
FileWriter;
import java
io
IOException;
import java
util
Iterator;
import java
util
List;
import org
dom
j
Attribute;
import org
dom
j
Document;
import org
dom
j
DocumentException;
import org
dom
j
DocumentHelper;
import org
dom
j
Element;
import org
dom
j
Node;
import org
dom
j
io
OutputFormat;
import org
dom
j
io
SAXReader;
import org
dom
j
io
XMLWriter;
public class DOM
JTest {
public static void main(String[] args) {
//獲得DOM
J以SAX方式處理XML文檔的管道
SAXReader reader = new SAXReader()
try {
//通過管道獲得XML文檔對象
Document document = reader
read(new File(
User
hbm
xml
))
//獲取根節點
Element root = document
getRootElement()
System
out
println(root
getName())
Element element = null;
//遍歷根節點下的節點
for(Iterator<Element> i = root
elementIterator()
i
hasNext()
){
//根節點的子節點
element = (Element) i
next()
System
out
println(element
getName())
//遍歷節點屬性
for(Iterator<Attribute> j = element
attributeIterator()
j
hasNext()
){
Attribute attr = (Attribute)j
next()
System
out
println(attr
getName()+
:
+attr
getValue())
}
//遍歷名為
property
的節點
for(Iterator<Element> k = element
elementIterator(
property
)
k
hasNext()
){
Element e
= (Element)k
next()
System
out
println(e
getData())
}
}
//通過Xpath的方式尋找節點
List<Node> list = document
selectNodes(
//hibernate
mapping/class/property
)
for(Node n: list){
String name = n
valueOf(
@name
)
System
out
println(n
getName()+
@name:
+name)
}
//只拿第一個
Node node = document
selectSingleNode(
//hibernate
mapping/class
)
String name = node
valueOf(
@name
)
String table = node
valueOf(
@table
)
System
out
println(node
getName()+
@name:
+name+
@table:
+table)
createDocument()
} catch (DocumentException e) {
e
printStackTrace()
}
}
public static Document createDocument(){
//創建document對象
Document document = DocumentHelper
createDocument()
Element root = document
addElement(
hibernate
mapping
)
//鏈式編程
Element classElement = root
addElement(
class
)
addAttribute(
name
com
upcgrid
User
)
addAttribute(
table
t_user
)
classElement
addElement(
property
)
addAttribute(
name
username
)
addText(
aaa
)
classElement
addElement(
Property
)
addAttribute(
name
password
)
addText(
bbb
)
try {
FileWriter out = new FileWriter(
User
hbm
xml
)
document
write(out)
out
flush()
out
close()
} catch (IOException e) {
e
printStackTrace()
}
try {
//設定美化格式
OutputFormat format = OutputFormat
createPrettyPrint()
XMLWriter writer = new XMLWriter(new FileWriter(
User
hbm
xml
)
format)
writer
write(document)
writer
close()
//縮減格式
} catch (IOException e) {
e
printStackTrace()
}
return document;
}
}
輸出
hibernate
mapping
class
name:com
upcgrid
User
table:t_user
aaa
bbb
property @name:username
property @name:password
class @name:com
upcgrid
User
@table:t_user
User
hbm
xml
[html] view plaincopyprint?
<?xml version=
encoding=
UTF
?>
<hibernate
mapping><class name=
com
upcgrid
User
table=
t_user
><property name=
username
>aaa</property><Property name=
password
>bbb</Property></class></hibernate
mapping>
<?xml version=
encoding=
UTF
?>
<hibernate
mapping><class name=
com
upcgrid
User
table=
t_user
><property name=
username
>aaa</property><Property name=
password
>bbb</Property></class></hibernate
mapping>
User
hbm
xml
[html] view plaincopyprint?
<?xml version=
encoding=
UTF
?>
<hibernate
mapping>
<class name=
com
upcgrid
User
table=
t_user
>
<property name=
username
>aaa</property>
<Property name=
password
>bbb</Property>
</class>
</hibernate
mapping>
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26492.html