XML文件 ?xml version=1.0 encoding=UTF-8?bookstorebook id=1name冰与火之歌/nameauthor乔治马丁/authoryear2014/yearprice89/price/bookbook id=2name安徒生童话/nameyear2004/yearprice77/pricelanguageEnglish/language/book/bookstore ha
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book id="1"> <name>冰与火之歌</name> <author>乔治马丁</author> <year>2014</year> <price>89</price> </book> <book id="2"> <name>安徒生童话</name> <year>2004</year> <price>77</price> <language>English</language> </book> </bookstore>
handler.java类
package xmltes; import javax.xml.stream.events.Characters; import javax.xml.stream.events.StartElement; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class handler extends DefaultHandler{ private static int bookNum = 0; //遍历xml文件的开始标签 @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // 调用DefaultHandler父类的startElement方法 super.startElement(uri, localName, qName, attributes); //判断是不是有属性的标签/如book if(qName.equals("book")){ bookNum++; System.out.println("====================开始遍历第"+bookNum+"书的内容================"); //开始解析book元素 //已知book元素下属性的名称,可以根据名称判断 /* String value = attributes.getValue("id"); System.out.println("book的属性值是" + value);*/ //如果不知道数的属性名称 int num = attributes.getLength(); for(int i=0;i<num;i++){ System.out.println("书的第" + (i+1) + "个属性名是:"+attributes.getQName(i)+"===属性值是:" + attributes.getValue(i)); } }else if(!qName.equals("book") && !qName.equals("bookstore") ){ System.out.println("节点名是:" + qName); } } //遍历xml文件的结束标签 @Override public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub super.endElement(uri, localName, qName); //判断这本书是否遍历结束 if(qName.equals("book")){ System.out.println("====================结束遍历第"+bookNum+"书的内容================"); } } @Override public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub super.characters(ch, start, length); String value = new String(ch, start, length); if(!value.trim().equals("")){ System.out.println(value); } } //用来标识解析开始 @Override public void startDocument() throws SAXException { // TODO Auto-generated method stub super.startDocument(); // System.out.println("进入标识解析开始方法"); } //用来标识解析结束 @Override public void endDocument() throws SAXException { // TODO Auto-generated method stub super.endDocument(); // System.out.println("进入标识解析结束方法"); } }
Test类
package xmltes; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; public class SAXTest { public static void main(String[] args) { try { //获取一个SAXParserFactory的实例 SAXParserFactory factory = SAXParserFactory.newInstance(); //通过factory获取SAXParser实例 SAXParser parser = factory.newSAXParser(); //创建一个SAXParserHandler对象 handler handler = new handler(); parser.parse("001.xml", handler); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
以上就是XML文件使用SAX方法读取的示例代码的详细内容,更多请关注自由互联其它相关文章!