需要下载一个xml解析器,如果你用的是jb,就可以直接用xerces。然后自己写代码读出值来。
下面是我的一个例子,你按自己的要求修改即可。
package com.san.share.pubutil;import java.io.*;
import java.net.URL;import org.apache.xerces.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;public class ReadXmlData {
private Document doc;
private static ReadXmlData instance ;
//the file must be stored at the same path where the class file stored
private static final String xmlFileName="datasource.xml";
private ReadXmlData() {
loadXml(xmlFileName);//get the datasource.xml as the config file, and only give the file name not full path name. } public static ReadXmlData getInstance() {
if(instance == null) {
instance = new ReadXmlData();
}
return instance;
}
private void loadXml(String fileName) {
DOMParser parser = new DOMParser();
try {
InputSource source = new InputSource(getClass().getClassLoader().getResourceAsStream(fileName));
// InputSource source = new InputSource(new FileReader(fileName));
parser.parse(source);
doc = parser.getDocument();
}catch(IOException ex){
SysLog.error("在读取xml文件时IO出错!");
SysLog.logex(ex);
}
catch(SAXException ex){
// ex.printStackTrace();
SysLog.logex(ex);
}
} /**
* This method is not safety, if the key that you provide is not exists in the xml file
* it would throw NullPointerException.
*
* @param key
* @return
*/
public String getConfig(String key) {
// Node result = doc.getElementsByTagName(key).item(0);
// return result.getFirstChild().getNodeValue();
//use the key as the default return value.
return getConfig(key, key);
} public String getConfig(String key, String defaultValue) {
Node result = doc.getElementsByTagName(key).item(0);
if(result == null){
return defaultValue;
}
return result.getFirstChild().getNodeValue();
}
}
下面是我的一个例子,你按自己的要求修改即可。
package com.san.share.pubutil;import java.io.*;
import java.net.URL;import org.apache.xerces.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;public class ReadXmlData {
private Document doc;
private static ReadXmlData instance ;
//the file must be stored at the same path where the class file stored
private static final String xmlFileName="datasource.xml";
private ReadXmlData() {
loadXml(xmlFileName);//get the datasource.xml as the config file, and only give the file name not full path name. } public static ReadXmlData getInstance() {
if(instance == null) {
instance = new ReadXmlData();
}
return instance;
}
private void loadXml(String fileName) {
DOMParser parser = new DOMParser();
try {
InputSource source = new InputSource(getClass().getClassLoader().getResourceAsStream(fileName));
// InputSource source = new InputSource(new FileReader(fileName));
parser.parse(source);
doc = parser.getDocument();
}catch(IOException ex){
SysLog.error("在读取xml文件时IO出错!");
SysLog.logex(ex);
}
catch(SAXException ex){
// ex.printStackTrace();
SysLog.logex(ex);
}
} /**
* This method is not safety, if the key that you provide is not exists in the xml file
* it would throw NullPointerException.
*
* @param key
* @return
*/
public String getConfig(String key) {
// Node result = doc.getElementsByTagName(key).item(0);
// return result.getFirstChild().getNodeValue();
//use the key as the default return value.
return getConfig(key, key);
} public String getConfig(String key, String defaultValue) {
Node result = doc.getElementsByTagName(key).item(0);
if(result == null){
return defaultValue;
}
return result.getFirstChild().getNodeValue();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货