利用 Jaxp (Sax) 解析xml文件--- 解析指定节点的值
2009-11-22 11:30
381 查看
package tao.test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SqlServer {
private String username;
private String password;
private String url;
private String driver;
/**
* 存储结果
*/
Map map = new HashMap();
/**
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
*/
public SqlServer() throws SAXException, IOException, ParserConfigurationException {
String userDir = System.getProperty("user.dir");
String eosConfig = new File(userDir).getParent()
+ "/eosserver/config/eosconfig.xml";
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
// MyHander 是一个内部类
sp.parse(new File(eosConfig), new MyHandler());
this.username = map.get("username").toString().trim();
this.password = map.get("password").toString().trim();
this.url = map.get("jdbcurl").toString().trim();
this.driver = map.get("jdbcdriver").toString().trim();
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
/**
*
*/
class MyHandler extends DefaultHandler {
List keys = new ArrayList() {
{
add("username");
add("password");
add("jdbcurl");
add("jdbcdriver");
}
};
String key = null;
private int index = 0;
private int flag = index;
public void characters(char ch[], int start, int length)
throws SAXException {
String value = new String(ch, start, length);
if (flag == index - 1) {
map.put(key, value);
flag = index;
}
}
public void startElement(String uri, String localName, String qName,
Attributes attr) throws SAXException {
if ("configValue".equals(qName)) {
if (keys.contains(attr.getValue("key"))) {
key = attr.getValue("key");
index++;
}
}
}
}
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
new SqlServer();
}
}
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SqlServer {
private String username;
private String password;
private String url;
private String driver;
/**
* 存储结果
*/
Map map = new HashMap();
/**
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
*/
public SqlServer() throws SAXException, IOException, ParserConfigurationException {
String userDir = System.getProperty("user.dir");
String eosConfig = new File(userDir).getParent()
+ "/eosserver/config/eosconfig.xml";
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
// MyHander 是一个内部类
sp.parse(new File(eosConfig), new MyHandler());
this.username = map.get("username").toString().trim();
this.password = map.get("password").toString().trim();
this.url = map.get("jdbcurl").toString().trim();
this.driver = map.get("jdbcdriver").toString().trim();
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
/**
*
*/
class MyHandler extends DefaultHandler {
List keys = new ArrayList() {
{
add("username");
add("password");
add("jdbcurl");
add("jdbcdriver");
}
};
String key = null;
private int index = 0;
private int flag = index;
public void characters(char ch[], int start, int length)
throws SAXException {
String value = new String(ch, start, length);
if (flag == index - 1) {
map.put(key, value);
flag = index;
}
}
public void startElement(String uri, String localName, String qName,
Attributes attr) throws SAXException {
if ("configValue".equals(qName)) {
if (keys.contains(attr.getValue("key"))) {
key = attr.getValue("key");
index++;
}
}
}
}
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
new SqlServer();
}
}
相关文章推荐
- 【慕课笔记】3-3 应用SAX方式解析XML—使用SAX解析XML文件的节点名和节点间文本
- 利用SAX对XML文件解析
- jaxp通过sax方式解析xml文件
- java语言连接mysql数据库并利用XML解析工具DOM生成XML文件,然后利用DOM SAX对所生成XML文件里的详细信息进行解析
- 用JAXP的SAX方式解析XML文件
- 利用JAXP的SAX方式对xml进行解析
- Android中利用SAX解析XML文件(转)
- jaxp利用Sax解析XML
- 利用Dom,Sax,Pull三种方式解析xml文件
- 个人安卓学习笔记---分别利用SAX、DOM和Pull实现对XML文件的解析并进行单元测试
- android应用开发之利用SAX、DOM和Pull实现对XML文件的解析并进行单元测试
- 用Jaxp包中的SAX技术对XML文件进行解析并完成对文件的读取操作.doc
- 【慕课笔记】3-2 应用SAX方式解析XML—使用SAX解析XML文件的节点属性
- JAXP之SAX解析的第一种方法,利用SAXParser.parser()方法
- 利用JAXP开发包进行SAX方式进行解析
- 使用SAX或者DOM或者pull解析XML文件的使用
- Android:利用xstream解析xml文件
- JAXP进行DOM和SAX解析
- DOM、SAX、JDOM和DOM4J四种方式解析XML文件性能对比
- 解析xml文件,遍历输出xml文件中的所有节点, 最终模仿实现struts2框架