标签:
<?xml version="1.0" encoding="UTF-8"?> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> <CD> <TITLE>Hide your heart</TITLE> <ARTIST>Bonnie Tyler</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>CBS Records</COMPANY> <PRICE>9.90</PRICE> <YEAR>1988</YEAR> </CD> <CD> <TITLE>Greatest Hits</TITLE> <ARTIST>Dolly Parton</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>RCA</COMPANY> <PRICE>9.90</PRICE> <YEAR>1982</YEAR> </CD> <CD> <TITLE>Still got the blues</TITLE> <ARTIST>Gary Moore</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>Virgin records</COMPANY> <PRICE>10.20</PRICE> <YEAR>1990</YEAR> </CD> <CD> <TITLE>Eros</TITLE> <ARTIST>Eros Ramazzotti</ARTIST> <COUNTRY>EU</COUNTRY> <COMPANY>BMG</COMPANY> <PRICE>9.90</PRICE> <YEAR>1997</YEAR> </CD> <CD> <TITLE>One night only</TITLE> <ARTIST>Bee Gees</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>Polydor</COMPANY> <PRICE>10.90</PRICE> <YEAR>1998</YEAR> </CD> </CATALOG>
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.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SaxParse { public static void main(String[] args) { SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); try { SAXParser saxParser = saxParserFactory.newSAXParser(); MyHandler myHandler=new MyHandler(); saxParser.parse("cd.xml", myHandler); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class MyHandler extends DefaultHandler{//继承DefaultHandler,然后重写父类方法 //两个类成员,用于在方法之间保存数据 boolean flag=false; String artist; @Override public void startDocument() throws SAXException { //System.out.println("MyHandler.startDocument()====>开始解析文档"); } @Override public void endDocument() throws SAXException { //System.out.println("MyHandler.endDocument()====>结束解析文档"); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { //System.out.println("MyHandler.startElement()====>开始解析元素"); //解析到了<ARTIST>这一元素,传递一个信号给characters()方法,将每个作者都保存到artist if (qName.equals("ARTIST")) { flag=true; } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { //System.out.println("MyHandler.endElement()====>结束解析元素"); } @Override public void characters(char[] ch, int start, int length) throws SAXException { //System.out.println("MyHandler.characters()====>得到元素的文本"); //解析到了<ARTIST>这一元素,将每个作者都保存到artist if (flag) { artist=new String(ch, start, length); flag=false; } //如果<COUNTRY>元素的文本是UK,就输出作者名字 String uk=new String(ch, start, length); if (uk.equals("UK")) { System.out.println("The artist from UK----->"+artist); } } }
标签:
原文地址:http://www.cnblogs.com/linchaohao/p/5143843.html