package com.cyanseed.frame.util;import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
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 SaxParseService extends DefaultHandler{  private Map<String, String> map  ;
private String qName ;
private Attributes attributes ;
private  String name ;
private String newName ;
private Boolean b = false ;
public Map<String, String> getMap() {
if(map.size()==0)
return null;
return map;
}
/**
 *  
 * @param file XML文件
 * @param name 属性名称; 当传NUll时返回所有,返回Map对象  key值为或标签名称(多个同样标签,在一起 ,返回标签名递加一),Boolean失效
 * @param b 控制是否得到Id属性值
 * @return   返回Map对象  key值为ID(或者属性名,由boolean控制),注意得到的MAP最好判断下是否为空
 * 这里返回的都是标签下面有值的标签
 * @throws SAXException 
 * @throws IOException
 * @throws ParserConfigurationException
 */
public Map<String, String> getXMLValue(File file,String name,Boolean b) throws SAXException, IOException, ParserConfigurationException{
SAXParserFactory factory = SAXParserFactory.newInstance();   
SAXParser parser = factory.newSAXParser();   
SaxParseService handler = new SaxParseService();  
handler.name = name ;
handler.map = new LinkedHashMap<String, String>() ;
if(b!=null)
handler.b = b ;
parser.parse( file, handler);  return handler.getMap() ;
} @Override  
public void startDocument() throws SAXException {    }    @Override  
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {   
this.attributes = attributes ;
this.qName = qName ;
}   @Override  
public void endElement(String uri, String localName, String qName)   
throws SAXException {   
qName = null ;
attributes = null ;
/**当解析结束时置为空。这里很重要,例如,当图中画3的位置结束后,会调用这个方法  
        ,如果这里不把preTag置为null,根据startElement(....)方法,preTag的值还是book,当文档顺序读到图  
        中标记4的位置时,会执行characters(char[] ch, int start, int length)这个方法,而characters(....)方  
                法判断preTag!=null,会执行if判断的代码,这样就会把空值赋值给book,这不是我们想要的。*/  
}    int i  = 0 ;
@Override  
public void characters(char[] ch, int start, int length) throws SAXException {   
if(qName !=null){
if(name ==null){
if(newName==null){
newName = qName ;
}
String s  =  new String(ch,start,length) ;
String s2  =  new String(ch,start,length) ;
if(s!=null){
s = s.replaceAll("\t", "");
s = s.replaceAll("\n", "");
s = s.replaceAll(" ", "");
if(!s.equals("")){
if(!newName.equals(qName)){
i= 0 ;
}
if(i==0)
map.put(qName, s) ;
else
map.put(qName+i, s2) ;
i = i+1 ; newName = qName ;
}
}
} else{
if(qName.equals(name)){ String s  =  new String(ch,start,length) ;
String s2  =  new String(ch,start,length) ;
if(s!=null){
s = s.replaceAll("\t", "");
s = s.replaceAll("\n", "");
s = s.replaceAll(" ", "");
if(!s.equals("")){ if(b){
map.put(attributes.getValue(0), s2) ;
}else{
if(i==0)
map.put(name, s) ;
else
map.put(name+i, s2) ;
i = i+1 ;
}
}
}
}
}
}
}}