java使用SAX解析XML
来源:互联网 发布:裴乐品牌知乎 编辑:程序博客网 时间:2024/06/05 15:59
源代码文件:
package com.qydev.mifeng.sax;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List;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 XMLParser { public static void main(String[] args) throws SAXException, IOException{// System.out.println(System.getProperty("user.dir"));// File f = new File(".");// String absolutePath = f.getAbsolutePath();// System.out.println(absolutePath);// //this.getClass().// // File f1 = null;// try{// f1 = new File("mm/a.text");// if(!f1.getParentFile().exists())// f1.getParentFile().mkdirs();// //f1.getParent().m// f1.getParentFile().mkdirs();// if(!f1.getParentFile().exists())// //f1.getParentFile().mkdirs();// f1.createNewFile();// // }catch(FileNotFoundException e){// System.out.println("exception isisi");// f1.getParentFile().mkdirs();// }// FileOutputStream fos = new FileOutputStream(f1);// fos.write("welcome to shanghai".getBytes());// f1.createNewFile(); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = null; try { parser = factory.newSAXParser(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } XmlContentParser xmlContentParser = new XmlContentParser(); //parser.parse(new File("book.xml"), xmlContentParser); //parser.parse(new File(XMLParser.class.getResource("/com/qydev/mifeng/sax/book.xml").getPath()), xmlContentParser); parser.parse(new File("src/com/qydev/mifeng/sax/book.xml"), xmlContentParser); List<Book> bookList = xmlContentParser.getBookList(); for(Book book:bookList){ System.out.println(book); } } public static class XmlContentParser extends DefaultHandler{ private Book book; private List<Book> books = new ArrayList<Book>(); private String preTag; @Override public void startDocument() throws SAXException { super.startDocument(); System.out.println("start the document: "); } @Override public void endDocument() throws SAXException { super.endDocument(); System.out.println("end the document: "); System.out.println("XML文件解析结束..."); System.out.println("结果:" + books); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); if(qName.equals("book") && book == null){ book = new Book(); } preTag = qName; } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); if("book".equals(qName) && book!=null){ books.add(book); book = null; } } @Override public void characters(char[] ch, int start, int length) throws SAXException { super.characters(ch, start, length); System.out.println("characters-----------"); // System.out.println(" ch: " + Arrays.toString(ch) ); System.out.println(" ch: " + ch); System.out.println(" start: " + start); System.out.println(" length: " + length); if(book != null && preTag != null && !"book".equals(preTag) && !"shelf".equals(preTag) ){ String data = new String(ch, start, length); if(!"".equals(data.trim())){ if("name".equals(preTag)){ book.setBookName(data); } else if("author".equals(preTag)){ book.setAutor(data); } else if("price".equals(preTag)){ book.setPrice(Integer.valueOf(data)); } } } } public List<Book> getBookList(){ return books; } } public static class Book{ private String bookName; private String autor; private int price; public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; //this.getClass().getResource(name) } public String getAutor() { return autor; } public void setAutor(String autor) { this.autor = autor; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String toString(){ return "book name: "+ getBookName()+", book author: "+getAutor()+", book price: "+getPrice(); } }}
XML文件:
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE 书架 SYSTEM "book.dtd"><!-- <书架> <书> <书名>xxx</书名> <作者>jjf</作者> <售价>23</售价> </书> <书> <书名>yyy</书名> <作者>蜜蜂</作者> <售价>12</售价> </书></书架> --><shelf> <book> <name>xxx</name> <author>jjf</author> <price>23</price> </book> <book> <name>yyy</name> <author>蜜蜂</author> <price>12</price> </book></shelf>
References:
1. http://blog.sina.com.cn/s/blog_8a86f4dd0101edv1.html
2. https://yq.aliyun.com/ziliao/37597
3. http://lavasoft.blog.51cto.com/62575/265821/
4. http://blog.sina.com.cn/s/blog_9386f17b0100w2vv.html
5. http://blog.csdn.net/chindroid/article/details/7735832/
阅读全文
0 0
- java使用sax解析xml
- java使用sax解析xml
- java使用SAX解析XML
- Java XML解析 - 使用SAX 示例
- Java 使用SAX解析xml文件
- Java 使用SAX解析xml文件
- java中使用SAX解析XML
- 在java中使用sax解析xml
- Java 中使用 SAX 解析 XML 文档
- Java使用Sax解析xml文件
- JAVA-使用SAX解析XML数据
- java使用SAX接口解析xml文件
- 使用SAX解析XML
- 使用SAX解析XML
- 使用SAX解析XML
- 使用SAX解析XML
- 使用SAX解析XML
- 使用SAX解析XML
- DB2 学习笔记 ——数据库管理
- NOIP2016day2t2 蚯蚓
- Codeforces Round #426 (Div. 2)The Meaningless Game(思维+二分)
- 问题总结
- A. Vasya and Robot----思维题
- java使用SAX解析XML
- HDFS
- Codeforces Round #426 (Div. 2)The Meaningless Game(思维+二分)
- C. Vasya and Beautiful Arrays----思维题
- [PAT乙级]1022. D进制的A+B (20)
- (Codeforces Round #426 (Div. 2)) C.The Meaningless Game
- Codeforces Round #426 (Div. 2) B. The Festive Evening(思维)
- java操作Redis(哨兵或单机模式)
- Linux下Nginx的安装