JAVA编码(29)——创建及解析XML文档技术
完场项目在 360云盘:经典详例Demo:创建及解析xml技术
导入jar包:
dom4j-1.6.1.jar
jaxen-1.1-beta-6.jar
项目根目录下,和src并列:创建 User.hbm.xml
<hibernate-mapping><class name="com.bjsxt.user" table="t_user">
<property name="username"></property>
<property name="password"></property>
</class>
</hibernate-mapping>
package com.sinosoft.dom4j;import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class TestDom4j {
private static final String fileName = "User.hbm.xml";
/**
* 工作中创建xml文档,最常用的framework
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//解析xml文档
parseXmlDocument();
//创建xml文档
createXmlDocument();
}
/**
* 创建xml文档
* @throws IOException
*/
private static void createXmlDocument() throws IOException {
Document document = DocumentHelper.createDocument();
Element root = document.addElement("hibernate-mapping");
Element classElement = root.addElement("class")
.addAttribute("name", "com.sinosoft.User")
.addAttribute("table", "t_user");
classElement.addElement("property").addAttribute("name", "username");
//普通写出,没有格式
// FileWriter out = new FileWriter("User1.hbm.xml");
// document.write(out);
// out.flush();
// out.close();
//创建输出格式
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(
new FileWriter("User1.hbm.xml"), format);
writer.write(document);
writer.close();
}
/**
* 解析xml文档
*/
public static void parseXmlDocument(){
Document document = getDocument(fileName);
Element rootElement = getRootElement(document);
System.out.println("根节点名称:"+rootElement.getName());
/**
* 获取根节点-->子节点元素
*/
for ( Iterator i = rootElement.elementIterator(); i.hasNext(); ) {
Element element = (Element) i.next();
System.out.println("根节点下子节点名称:"+element.getName());
/**
* 获取根节点-->子节点属性
*/
for ( Iterator j = element.attributeIterator(); j.hasNext(); ) {
Attribute attribute = (Attribute) j.next();
System.out.println("属性名称:"+attribute.getName()+" 属性值:"+attribute.getValue());
}
/**
* 获取根节点-->子节点-->子节点元素
*/
for (Iterator j = element.elementIterator(); j.hasNext();) {
Element childElement = (Element) j.next();
System.out.println("子节点下子节点:"+childElement.getName());
/**
* 获取根节点-->子节点-->子节点属性
*/
for (Iterator k = childElement.attributeIterator(); k.hasNext();) {
Attribute childaAttribute = (Attribute) k.next();
System.out.println("子节点下子节点属性名:"+childaAttribute.getName()+" 子节点下子节点属性值:"+childaAttribute.getValue());
}
}
}
//xpath
System.out.println("使用xpath进行xml文档的解析");
List<Node> list = document.selectNodes("//hibernate-mapping/class/property");
for (Node node : list) {
System.out.println("节点:"+node.getName());
System.out.println("节点中name的属性值:"+node.valueOf("@name"));
}
}
/**
* 获取文档对象
* 所谓的 SAX 即为读一点解析一点
* @param fileName
* @return
*/
public static Document getDocument(String fileName){
Document document = null;
try {
SAXReader reader = new SAXReader();
document = reader.read(new File(fileName));
} catch (DocumentException e) {
e.printStackTrace();
}
return document;
}
/**
* 获取文档节点的根节点
* Element : 元素
* @param document
* @return
*/
public static Element getRootElement(Document document){
return document.getRootElement();
}
}
以上是 JAVA编码(29)——创建及解析XML文档技术 的全部内容, 来源链接: utcz.com/z/394683.html