首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

sax解析 失去xml文档所有内容和获取指定标签的值

2013-01-28 
sax解析得到xml文档所有内容和获取指定标签的值//得到xml文档所有内容package cn.itcast.saximport java.

sax解析 得到xml文档所有内容和获取指定标签的值

//得到xml文档所有内容package cn.itcast.sax;import java.io.IOException;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;import org.xml.sax.ContentHandler;import org.xml.sax.Locator;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;public class test1 {/** * sax解析xml文档 * @throws SAXException  * @throws ParserConfigurationException  * @throws IOException  */public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {//1.创建解析工厂SAXParserFactory factory = SAXParserFactory.newInstance();//2.得到解析器SAXParser sp = factory.newSAXParser();//3.得到读取器XMLReader reader = sp.getXMLReader();//4.设置内容处理器reader.setContentHandler(new ListHandler());//5.读取xml文档内容reader.parse("src/book.xml");}}//得到xml文档所有内容class ListHandler implements ContentHandler{@Overridepublic void startElement(String uri, String localName, String qName,Attributes atts) throws SAXException {System.out.println("<" + qName + ">");for(int i = 0; atts != null && i < atts.getLength(); i++){String attName = atts.getQName(i);String attValue = atts.getValue(i);System.out.println(attName + "=" + attValue);}}@Overridepublic void characters(char[] ch, int start, int length)throws SAXException {System.out.println(new String(ch,start,length));}@Overridepublic void endElement(String uri, String localName, String qName)throws SAXException {System.out.println("</" + qName + ">");}@Overridepublic void setDocumentLocator(Locator locator) {// TODO Auto-generated method stub}@Overridepublic void startDocument() throws SAXException {// TODO Auto-generated method stub}@Overridepublic void endDocument() throws SAXException {// TODO Auto-generated method stub}@Overridepublic void startPrefixMapping(String prefix, String uri)throws SAXException {// TODO Auto-generated method stub}@Overridepublic void endPrefixMapping(String prefix) throws SAXException {// TODO Auto-generated method stub}@Overridepublic void ignorableWhitespace(char[] ch, int start, int length)throws SAXException {// TODO Auto-generated method stub}@Overridepublic void processingInstruction(String target, String data)throws SAXException {// TODO Auto-generated method stub}@Overridepublic void skippedEntity(String name) throws SAXException {// TODO Auto-generated method stub}}//获取指定标签的值package cn.itcast.sax;import java.io.IOException;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;import org.xml.sax.ContentHandler;import org.xml.sax.Locator;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.DefaultHandler;public class Text2 {/** * sax解析xml文档 * @throws SAXException  * @throws ParserConfigurationException  * @throws IOException  */public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {//1.创建解析工厂SAXParserFactory factory = SAXParserFactory.newInstance();//2.得到解析器SAXParser sp = factory.newSAXParser();//3.得到读取器XMLReader reader = sp.getXMLReader();//4.设置内容处理器reader.setContentHandler(new TagValueHandler());//5.读取xml文档内容reader.parse("src/book.xml");}}//获取指定标签的值class TagValueHandler extends DefaultHandler{private String currentTag;//记住当前解析的是什么标签private  int needNumber = 2;//记住想获取第几个作者标签的值private  int currentNumber;//当前解析的是第几个@Overridepublic void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {currentTag = qName;if(currentTag.equals("作者")){currentNumber++;}}@Overridepublic void characters(char[] ch, int start, int length)throws SAXException {if("作者".equals(currentTag) && currentNumber == needNumber){System.out.println(new String(ch,start,length));}}@Overridepublic void endElement(String uri, String localName, String qName)throws SAXException {currentTag = null;}}

热点排行