使用JDOM读取XML文件的简单示例
程序员文章站
2022-05-28 09:40:12
...
功能:
[b]1、使用JDOM读取Xml文件,包括Xpath读取单个节点或多个节点列表;
2、使用JDOM构建一个简单XML文档对象,并进行存储;XML模板文件[/b]
Java文件
[b]1、使用JDOM读取Xml文件,包括Xpath读取单个节点或多个节点列表;
2、使用JDOM构建一个简单XML文档对象,并进行存储;XML模板文件[/b]
<?xml version="1.0" encoding="UTF-8"?>
<configure>
<jdbcInfo>
<driver>oracle.jdbc.driver.OracleDriver</driver>
<url>jdbc:oracle:thin:@localhost:1521:orcl</url>
<username>scott</username>
<password>scottlxd</password>
</jdbcInfo>
<beans>
<bean id="beanDao" class="com.xdzj.dao.bean1"></bean>
<bean id="beanService" class="com.xdzj.service.bean2"></bean>
</beans>
</configure>
Java文件
package com.work;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;
public class JdomReadXml {
private static JdomReadXml jdomReadXml = null;
private static Element rootElement = null;
private JdomReadXml() {
}
public static JdomReadXml getInstance() {
if (jdomReadXml == null) {
jdomReadXml = new JdomReadXml();
}
return jdomReadXml;
}
/**
* 初始化文档对象
* @param filePath
*/
public void loadXmlResource(String filePath) {
SAXBuilder builder = new SAXBuilder();
try {
Document doc = builder.build(new File(filePath));
rootElement = doc.getRootElement();
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 一、使用JDOM读取单个的节点
* 采用Xpath路径读取
*/
public void initJdbcInfo() {
try {
Element driver = (Element) XPath.selectSingleNode(rootElement,
"//configure/jdbcInfo/driver");
System.out.println(driver.getText());
} catch (JDOMException e) {
e.printStackTrace();
}
}
/**
* 二、使用JDOM读取一个节点列表
*/
public void initBeans(){
try {
List<?> elementList = XPath.selectNodes(rootElement, "//configure/beans/bean");
for(Object e : elementList){
Element element = (Element)e;
String id = element.getAttributeValue("id");
String clszz = element.getAttributeValue("class");
System.out.print(id + "\t" + clszz);
System.out.println();
}
} catch (JDOMException e) {
e.printStackTrace();
}
}
/**
* 三、使用JDOM创建Xml文件
*/
public void createXmlExcample(){
Element root = new Element("selects");
Element select = new Element("select");
Element id = new Element("id");
// 设置值
id.addContent("1");
Element name = new Element("name");
name.addContent("小迪之家");
select.addContent(id);
select.addContent(name);
root.addContent(select);
Document document = new Document();
document.addContent(root);
// 使用XMLOutputter对象进行输出
XMLOutputter out = new XMLOutputter();
// 设置输出编码格式
out.setFormat(Format.getCompactFormat().setEncoding("GBK")); String docStr = out.outputString(document);
System.out.println(docStr);
try {
out.output(document, new FileOutputStream("src/com/test.xml"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 测试方法
* @param args
*/
public static void main(String[] args) {
String filePath = "src/com/jdomXmlTemplate.xml";
JdomReadXml jdomReadXml = JdomReadXml.getInstance();
jdomReadXml.loadXmlResource(filePath);
jdomReadXml.initJdbcInfo();
jdomReadXml.initBeans();
jdomReadXml.createXmlExcample();
}
}
上一篇: Ant中常见的Task(一)
下一篇: 单例对象属性的更新--影子实例