Home > Article > Backend Development > Sample code for reading XML files using SAX method
XMLFile
<?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 class
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 class
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(); } } }
The above is the detailed content of Sample code for reading XML files using SAX method. For more information, please follow other related articles on the PHP Chinese website!