由于在web和proj下,获取路径的不同导致,两种情况下解析xml的方式略有不同。下面以案例的形式来示范两者的不同点:
1.首先是建立xml文件如下所示:
xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 数据库配置 -->
<prop>
<mess>
<property name="mysql_db_driver" value="com.mysql.jdbc.Driver"></property>
<property name="cSMysqlServiceize" value="jdbc:mysql://localhost:3306/test"></property>
<property name="mysql_db_table" value="testresult"></property>
<property name="mysql_db_user" value="root"></property>
<property name="mysql_db_password" value="123456"></property>
</mess>
</prop>
2.建立xml文件之后,编写javabean解析xml文件:
2.1在proj情况下解析代码如下:
public static String ReadConfig(String name) throws JDOMException, IOException
{
try {
// 创建一个SAXBuilder对象
SAXBuilder saxBuilder = new SAXBuilder();
// 读取prop.xml资源
Document doc = saxBuilder.build("config/config.xml");
// 获取根元素(prop)
Element root = doc.getRootElement();
// 获取根元素下面的所有子元素(mess)
List<Element> messList = root.getChildren("mess");
// 子根元素(mess)
Element childrenRoot = null;
// property元素集合
List<Element> propertyList = null;
// 遍历根元素的子元素集合(即遍历mess元素)
for (int i = 0; i < messList.size(); i++) {
// 将根元素prop下的mess子元素作为一个新的子根元素
childrenRoot = messList.get(i);
// 获取子根元素mess下的所有property子元素
propertyList = childrenRoot.getChildren("property");
// 遍历子根元素的子元素集合(即遍历property元素)
for (int j = 0; j < propertyList.size(); j++) {
// 获取property元素
Element element = propertyList.get(j);
// element.getAttributeValue("name"):获取property中name属性的值
if (element.getAttributeValue("name").equals(name)) { // 如果name的值一致
return element.getAttributeValue("value"); // 取得name对应的value属性值
}
}
}
// 遍历完没有查找到结果返回null
return null;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
2.2 在web情况下解析如下:
为了实验需求将xml文件中的内容修改为:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 数据库配置 -->
<content>
<mess>
<mysql_db_driver>com.mysql.jdbc.Driver</mysql_db_driver>
<MysqlServiceize>jdbc:mysql://localhost:3306/test</MysqlServiceize>
<mysql_db_table>testresult</mysql_db_table>
<mysql_db_user>root</mysql_db_user>
<mysql_db_password>123456</mysql_db_password>
</mess>
</content>
然后是编写解析类:
package com.test.uti;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/*
* 201308161432
* */
public class ReadConfig {
// 配置单例
// private static ReadConfig instance = null;
// mysql配置项
private String mysql_db_driver;
private String mysql_db_service;
private String mysql_db_table;
private String mysql_db_user;
private String mysql_db_password;
private static String sstring = null;
public static String readconfig(String elementName,String nodeName,String fileName)
{
ReadConfig readcof=new ReadConfig();
DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance();
String path1 = readcof.getClass().getClassLoader().getResource(fileName).getPath();
String path=path1.replaceAll("%20", " ");//获取存放xml的文件路径
try {
DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder();
// Document document=documentBuilder.parse(path+File.separator+fileName);
// String linux_path=path.replaceAll("\\", "//");
// linux_path=linux_path.replaceAll(" ", "\\");
Document document=documentBuilder.parse(path);
NodeList nodeList=document.getElementsByTagName(nodeName);
if(nodeList.getLength()>0){
Element element=(Element) nodeList.item(0);
sstring=element.getElementsByTagName(elementName).item(0).getFirstChild().getNodeValue();
}
} catch (Exception e) {
// TODO: handle exception
}
return sstring;
}
public void setSqldriver(String mysql_db_driver) {
this.mysql_db_driver = mysql_db_driver;
}
public void setSqlservice(String mysql_db_service) {
this.mysql_db_service = mysql_db_service;
}
public void setSqltable(String mysql_db_table) {
this.mysql_db_table = mysql_db_table;
}
public void setSqluser(String mysql_db_user) {
this.mysql_db_user = mysql_db_user;
}
public void setSqlpassword(String mysql_db_password) {
this.mysql_db_password = mysql_db_password;
}
public String getSqldriver() {
return this.mysql_db_driver;
}
public String getSqlservice() {
return this.mysql_db_service;
}
public String getSqltable() {
return this.mysql_db_table;
}
public String getSqluser() {
return this.mysql_db_user;
}
public String getSqlpassword() {
return this.mysql_db_password;
}
}