public class SaxUtil_B {
public static Map getMyNodeValue(String filepath, List xpaths) {
SAXHander sh = null;
try {
SAXParserFactory sf = SAXParserFactory.newInstance(); // 通过工厂获得SAX解析器
SAXParser sax = sf.newSAXParser(); //解析器
sh = new SaxUtil_B().new SAXHander(xpaths); // 通过解析器解析xml文件
sax.parse(filepath, sh); //使用自定义的监听器
} catch (Exception e) {
e.printStackTrace();
}
return sh.m;
}
//自定义sax解析监听器
class SAXHander extends DefaultHandler {
private List ipaths = null;
private String[][] pps = null;
private int iii[] = null;
private int lsize = 0;
private Map m = new HashMap();
public SAXHander(List paths) {
super();
this.ipaths = paths;
init();
}
private void init() {
if (ipaths != null && ipaths.size() > 0) {
lsize = ipaths.size(); //节点路径数
/** 注意 pps和iii的长度是相同的---lsize(即xpath数)**/
pps = new String[lsize][]; //节点路径拆分后存储数组
iii = new int[lsize];
for (int i = 0; i < ipaths.size(); i++) {
String pp = (String) ipaths.get(i);
pps[i] = pp.split("/");
//pps_p[i]=pp;
iii[i] = 0;
}
}
}
public void startDocument() throws SAXException {
//开始处理文档
}
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (lsize > 0) {
for (int j = 0; j < pps.length; j++) {
if (pps[j] != null && pps[j].length > 0) {
for (int jj = 0; jj < pps[j].length; jj++) { //同一path的不同节点
if (qName.equals(pps[j][jj])) {
if (j <= iii.length && iii[j] == jj) {
iii[j]++;
}
}
}
}
}
}
}
public void characters(char[] ch, int start, int length) throws SAXException {
String myvalue = new String(ch, start, length);
//去掉xml文件中的空格节点
if (myvalue.trim().equals("")) {
return;
}
if (lsize > 0) {
for (int i = 0; i < iii.length; i++) {
if (iii[i] == pps[i].length) {
m.put(ipaths.get(i), myvalue);//get value
iii[i] = -1;
}
}
}
}
public void endElement(String uri, String localName, String qName) throws SAXException {
// System.out.println("元素结束 :" + qName);
}
public void endDocument() throws SAXException {
//文档处理结束
}
}
}
修改后(增加一个供调用方法):
public class SaxUtil {
public SAXParser getSaxParse(){
SAXParser sax=null;
try {
SAXParserFactory sf = SAXParserFactory.newInstance();
sax = sf.newSAXParser(); // 通过工厂获得SAX解析器
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
return sax;
}
public static Map getMyNodeValue(String filepath, List xpaths,SAXParser sax) {
SAXHander sh = null;
try {
sh = new SaxUtil().new SAXHander(xpaths); //使用自定义的监听器
sax.parse(filepath, sh);// 通过解析器解析xml文件
} catch (Exception e) {
e.printStackTrace();
}
return sh.m;
}
public static String getRootNodeName(String filepath,SAXParser sax){
SAXHander_Simple sh=null;;
try {
sh = new SaxUtil().new SAXHander_Simple();// 使用自定义的监听器
sax.parse(filepath, sh); // 通过解析器解析xml文件
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sh.RootName;
}
class SAXHander_Simple extends DefaultHandler{
private String RootName=null;
private int i=0;
public void startDocument() throws SAXException { }
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(i==0){
RootName=qName;
i++;
}
}
public void characters(char[] ch, int start, int length) throws SAXException { return; }
public void endElement(String uri, String localName, String qName) throws SAXException { return; }
public void endDocument() throws SAXException { }
}
//自定义sax解析监听器
class SAXHander extends DefaultHandler {
private List ipaths = null;
private String[][] pps = null;
private int iii[] = null;
private int lsize = 0;
private Map m = new HashMap();
public SAXHander(List paths) {
super();
this.ipaths = paths;
init();
}
private void init() {
if (ipaths != null && ipaths.size() > 0) {
lsize = ipaths.size(); //节点路径数
/** 注意 pps和iii的长度是相同的---lsize(即xpath数)**/
pps = new String[lsize][]; //节点路径拆分后存储数组
iii = new int[lsize];
for (int i = 0; i < ipaths.size(); i++) {
String pp = (String) ipaths.get(i);
pps[i] = pp.split("/");
//pps_p[i]=pp;
iii[i] = 0;
}
}
}
public void startDocument() throws SAXException { }//开始处理文档
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (lsize > 0) {
for (int j = 0; j < pps.length; j++) {
if (pps[j] != null && pps[j].length > 0) {
for (int jj = 0; jj < pps[j].length; jj++) { //同一path的不同节点
if (qName.equals(pps[j][jj])) {
if (j <= iii.length && iii[j] == jj) {
iii[j]++;
}
}
}
}
}
}
}
public void characters(char[] ch, int start, int length) throws SAXException {
String myvalue = new String(ch, start, length);
//去掉xml文件中的空格节点
if (myvalue.trim().equals("")) {
return;
}
if (lsize > 0) {
for (int i = 0; i < iii.length; i++) {
if (iii[i] == pps[i].length) {
m.put(ipaths.get(i), myvalue);//get value
iii[i] = -1;
}
}
}
}
public void endElement(String uri, String localName, String qName) throws SAXException { return;}
public void endDocument() throws SAXException { }//文档处理结束
}
}
public class SaxUtil {
public SAXParser getSaxParse(){
SAXParser sax=null;
try {
SAXParserFactory sf = SAXParserFactory.newInstance();
sax = sf.newSAXParser(); // 通过工厂获得SAX解析器
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
return sax;
}
public static Map getMyNodeValue(String filepath, List xpaths,SAXParser sax) {
SAXHander sh = null;
try {
sh = new SaxUtil().new SAXHander(xpaths); //使用自定义的监听器
sax.parse(filepath, sh);// 通过解析器解析xml文件
} catch (Exception e) {
e.printStackTrace();
}
return sh.m;
}
public static String getRootNodeName(String filepath,SAXParser sax){
SAXHander_Simple sh=null;;
try {
sh = new SaxUtil().new SAXHander_Simple();// 使用自定义的监听器
sax.parse(filepath, sh); // 通过解析器解析xml文件
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sh.RootName;
}
class SAXHander_Simple extends DefaultHandler{
private String RootName=null;
private int i=0;
public void startDocument() throws SAXException { }
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(i==0){
RootName=qName;
i++;
}
}
public void characters(char[] ch, int start, int length) throws SAXException { return; }
public void endElement(String uri, String localName, String qName) throws SAXException { return; }
public void endDocument() throws SAXException { }
}
//自定义sax解析监听器
class SAXHander extends DefaultHandler {
private List ipaths = null;
private String[][] pps = null;
private int iii[] = null;
private int lsize = 0;
private Map m = new HashMap();
public SAXHander(List paths) {
super();
this.ipaths = paths;
init();
}
private void init() {
if (ipaths != null && ipaths.size() > 0) {
lsize = ipaths.size(); //节点路径数
/** 注意 pps和iii的长度是相同的---lsize(即xpath数)**/
pps = new String[lsize][]; //节点路径拆分后存储数组
iii = new int[lsize];
for (int i = 0; i < ipaths.size(); i++) {
String pp = (String) ipaths.get(i);
pps[i] = pp.split("/");
//pps_p[i]=pp;
iii[i] = 0;
}
}
}
public void startDocument() throws SAXException { }//开始处理文档
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (lsize > 0) {
for (int j = 0; j < pps.length; j++) {
if (pps[j] != null && pps[j].length > 0) {
for (int jj = 0; jj < pps[j].length; jj++) { //同一path的不同节点
if (qName.equals(pps[j][jj])) {
if (j <= iii.length && iii[j] == jj) {
iii[j]++;
}
}
}
}
}
}
}
public void characters(char[] ch, int start, int length) throws SAXException {
String myvalue = new String(ch, start, length);
//去掉xml文件中的空格节点
if (myvalue.trim().equals("")) {
return;
}
if (lsize > 0) {
for (int i = 0; i < iii.length; i++) {
if (iii[i] == pps[i].length) {
m.put(ipaths.get(i), myvalue);//get value
iii[i] = -1;
}
}
}
}
public void endElement(String uri, String localName, String qName) throws SAXException { return;}
public void endDocument() throws SAXException { }//文档处理结束
}
}

本文介绍了一个使用SAX解析XML文件的Java示例,包括获取指定XPath节点的值及获取根节点名称的方法实现。
594

被折叠的 条评论
为什么被折叠?



