moummm
2017-08-12 15:57
public class SAXParserHandler extends DefaultHandler { int bIndex = 0; private ArrayList<Book> bookList = new ArrayList<Book>(); Book book=null; String qValue = null; public ArrayList<Book> getBookList() { return bookList; } @Override public void startDocument() throws SAXException { super.startDocument(); System.out.println("开始解析XML文件"); } @Override public void endDocument() throws SAXException { super.endDocument(); System.out.println(); System.out.println("一共有"+bookList.size()+"本书"); for (int i=0;i<bookList.size();i++){ System.out.println("一共有"+(i+1)+"本书"); Book b = bookList.get(i); System.out.println("书名:"+b.getName()); System.out.println("作者:"+b.getAuthod()); System.out.println("年份:"+b.getYear()); System.out.println("价格:"+b.getPrice()); System.out.println("语言:"+b.getLanguage()); } System.out.println("结束解析XML文件"); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); if(qName.equals("book")){ bIndex++; System.out.println("开始遍历第"+bIndex+"本书"); // //已知属性的情况 // String value = attributes.getValue("id"); // System.out.println("book的id属性值为:"+value); //不知道属性的值 for (int i=0;i<attributes.getLength();i++){ System.out.println("第"+(i+1)+"个属性名为:"+attributes.getQName(i)); System.out.println("第"+(i+1)+"个属性值为:"+attributes.getValue(i)); } System.out.println("结束遍历第"+bIndex+"本书"); }else if(!qName.equals("bookstore")&&!qName.equals("name")){ System.out.print("节点名为:"+qName+"---"); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); if (qName.equals("book")){ bookList.add(book); book =null; System.out.println("开始保存第"+bIndex+"本书的内容。。。。。。"); } else if (qName.equals("id")){ book.setId(qValue); } else if (qName.equals("name")) { book.setName(qValue); } else if (qName.equals("authod")){ book.setAuthod(qValue); } else if (qName.equals("year")){ book.setYear(qValue); } else if (qName.equals("price")){ book.setPrice(qValue); }else if(qName.equals("language")){ book.setLanguage(qValue); } } @Override public void characters(char[] ch, int start, int length) throws SAXException { super.characters(ch, start, length); qValue = new String(ch,start,length); if(!qValue.trim().equals("")){ System.out.println("节点值为:"+qValue); } } }
public class SAXTest { public static void main(String[] args) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); SAXParserHandler handler = new SAXParserHandler(); parser.parse("books.xml",handler); } }
public class Book { private String id; private String name; private String authod; private String price; private String year; private String language; public Book(){} public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthod() { return authod; } public void setAuthod(String authod) { this.authod = authod; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getLanguage() { return language; } public void setLanguage(String language) { this.language = language; } }
求助,为什么SAXPaserHandler中的endElement中会报空指令异常?
你的startElement()方法里没有创建book对象,应该在 bIndex++;之后 创建一个 book= new Book();应该就对了,试试看看
Java眼中的XML---文件读取
83199 学习 · 431 问题
相似问题