赞
踩
Dom4j是dom4j.org出品的一个开源XML解析包。Dom4j是一个易用的、开源的库,用于XML,XPath和XSLT。它应用于Java平台,采用了Java集合框架并完全支持DOM,SAX和JAXP。Hibernate也是用它来读写配置文件的。
一、DOM4j的下载
dom4j-1.6.1.jar 下载:http://sourceforge.net/directory/os:windows/freshness:recently-updated/?q=dom4j
二、利用dom4j创建一个内容如下的xml文件SqlMapConfig.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
- "ibatis-3-config.dtd">
- <configuration>
- <environments default="development">
- <environment id="MySQL">
- <transactionManager type="JDBC">123</transactionManager>
- <dataSource type="POOLED">
- <property name="driver" value="com.mysql.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
- <property name="username" value="root"/>
- <property name="password" value="admin"/>
- </dataSource>
- </environment>
- </environments>
-
- <mappers>
- <mapper resource="com/pojo/sql/DepMap.xml"/>
- <mapper resource="com/pojo/sql/EmpMap.xml"/>
- </mappers>
- </configuration>
- public class CreateXML {
-
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- //创建一个输出流,指定创建的xml文件的位置
- OutputStream os = new FileOutputStream("d:/SqlMapConfig.xml");
-
- XMLWriter writer = new XMLWriter(os,OutputFormat.createPrettyPrint());
-
- //创建Document
- Document document = DocumentHelper.createDocument();
-
- Element configuration = document.addElement("configuration");
-
- Element environments = configuration.addElement("environments");
- environments.addAttribute("default", "development");
-
- Element environment = environments.addElement("environment");
- environment.addAttribute("id", "MySQL");
- Element transactionManager = environment.addElement("transactionManager");
- transactionManager.addAttribute("type", "JDBC");
- transactionManager.setText("123");
-
- Element dataSource = environment.addElement("dataSource");
- dataSource.addAttribute("type", "POOLED");
- Element driver = dataSource.addElement("property");
- driver.addAttribute("name", "driver");
- driver.addAttribute("value", "com.mysql.jdbc.Driver");
- Element url = dataSource.addElement("property");
- url.addAttribute("name", "url");
- url.addAttribute("value", "jdbc:mysql://127.0.0.1:3306/test");
- Element username = dataSource.addElement("property");
- username.addAttribute("name", "username");
- username.addAttribute("value", "root");
- Element password = dataSource.addElement("property");
- password.addAttribute("name", "password");
- password.addAttribute("value", "admin");
-
- Element mappers = configuration.addElement("mappers");
- Element mapper1 = mappers.addElement("mapper");
- mapper1.addAttribute("resource", "com/pojo/sql/DepMap.xml");
- Element mapper2 = mappers.addElement("mapper");
- mapper2.addAttribute("resource", "com/pojo/sql/EmpMap.xml");
- //保存xml
- writer.write(document);
- writer.close();
- }
- }
- public class GetNodes {
-
- /**方法一:
- * 遍历新建的xml,将driver、url、username、password的值读取出来
- * @param args
- * @throws IOException
- * @throws DocumentException
- */
- public static void main(String[] args) throws IOException, DocumentException {
- SAXReader reader = new SAXReader();
- Document document = reader.read("d:/SqlMapConfig.xml");
- List<Element> elements = document.selectNodes("/configuration/environments/environment/dataSource/property");
- String driver = "";
- String url = "";
- String username = "";
- String password = "";
- for (Element e : elements) {
- String name = e.attributeValue("name");
- String value = e.attributeValue("value");
- if("driver".equals(name)){
- driver = value;
- }else if("url".equals(name)){
- url = value;
- }else if("username".equals(name)){
- username = value;
- }else if("password".equals(name)){
- password = value;
- }
- }
- System.out.println("driver:"+driver+",url:"+url+",username:"+username+",password:"+password);
- }
- }
- public class GetNodes1 {
-
- /**方法二:
- * 将driver、url、username、password的值读取出来
- * @param args
- * @throws IOException
- * @throws DocumentException
- */
- public static void main(String[] args) throws IOException, DocumentException {
- SAXReader reader = new SAXReader();
- Document document = reader.read("d:/SqlMapConfig.xml");
- Element driverElement = (Element)document.selectSingleNode("/configuration/environments/environment/dataSource/property[@name='driver']");
- Element urlElement = (Element)document.selectSingleNode("/configuration/environments/environment/dataSource/property[@name='url']");
- Element usernameElement = (Element)document.selectSingleNode("/configuration/environments/environment/dataSource/property[@name='username']");
- Element passwordElement = (Element)document.selectSingleNode("/configuration/environments/environment/dataSource/property[@name='password']");
- String driver = driverElement.attributeValue("value");
- String url = urlElement.attributeValue("value");
- String username = usernameElement.attributeValue("value");
- String password = passwordElement.attributeValue("value");
-
- System.out.println("driver:"+driver+",url:"+url+",username:"+username+",password:"+password);
- }
- }
- public class UpdateNode {
-
- /**
- * 修改密码为123456
- * @param args
- * @throws DocumentException
- * @throws IOException
- */
- public static void main(String[] args) throws DocumentException, IOException {
- //读取并修改
- SAXReader reader = new SAXReader();
- Document document = reader.read("d:/SqlMapConfig.xml");
- Element passwordElement = (Element)document.selectSingleNode("/configuration/environments/environment/dataSource/property[@name='password']");
- passwordElement.addAttribute("value", "123456");
- //保存
- OutputStream os = new FileOutputStream("d:/SqlMapConfig.xml");
- XMLWriter writer = new XMLWriter(os,OutputFormat.createPrettyPrint());
- writer.write(document);
- writer.close();
- os.close();
- }
-
- }
- public class DeleteNode {
-
- /**
- * 删除密码结点
- * @param args
- * @throws DocumentException
- * @throws IOException
- */
- public static void main(String[] args) throws DocumentException, IOException {
- //找到结点并删除
- SAXReader reader = new SAXReader();
- Document document = reader.read("d:/SqlMapConfig.xml");
- Element passwordElement = (Element) document.selectSingleNode("/configuration/environments/environment/dataSource/property[@name='password']");
- passwordElement.getParent().remove(passwordElement);
-
- //保存
- OutputStream os = new FileOutputStream("d:/SqlMapConfig.xml");
- XMLWriter writer = new XMLWriter(os,OutputFormat.createPrettyPrint());
- writer.write(document);
- writer.close();
- os.close();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。