XML文档解析

前端之家收集整理的这篇文章主要介绍了XML文档解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

book.xml(需要解析的XML文档)


  1. <?xml version="1.0" encoding="UTf-8">
  2. <books>
  3. <book bookno="001">
  4. <title>大数据时代</title>
  5. <author>张三</author>
  6. <price>50</price>
  7. </book>
  8. <book bookno="002">
  9. <title>Andriod开发</title>
  10. <author>李四</author>
  11. <price>70</price>
  12. </book>
  13. </books>

Book.java(文档的构造)


  1. package bo;
  2.  
  3. public class Book {
  4. public String bookno;
  5. public String title;
  6. public String author;
  7. public double price;
  8. public String getBookno() {
  9. return bookno;
  10. }
  11. public void setBookno(String bookno) {
  12. this.bookno = bookno;
  13. }
  14. public String getTitle() {
  15. return title;
  16. }
  17. public void setTitle(String title) {
  18. this.title = title;
  19. }
  20. public String getAuthor() {
  21. return author;
  22. }
  23. public void setAuthor(String author) {
  24. this.author = author;
  25. }
  26. public double getPrice() {
  27. return price;
  28. }
  29. public void setPrice(double price) {
  30. this.price = price;
  31. }
  32. @Override
  33. public String toString() {
  34. return "Book [author=" + author + ",bookno=" + bookno + ",price=" + price
  35. + ",title=" + title + "]";
  36. }
  37. public Book() {
  38. super();
  39. // TODO Auto-generated constructor stub
  40. }
  41. public Book(String bookno,String title,String author,double price) {
  42. super();
  43. this.bookno = bookno;
  44. this.title = title;
  45. this.author = author;
  46. this.price = price;
  47. }
  48.  
  49. }
  50.  


XmlParse.java(接口类,供SaxParse调用)


  1. package bo;
  2. import java.util.List;
  3. public interface XmlParse
  4. {
  5. public List<Book> xmlparse(String fileName);
  6. }
  7.  



Mysax.java(解析的功能实现类)


  1. package sax;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.xml.sax.Attributes;
  5. import org.xml.sax.SAXException;
  6. import org.xml.sax.helpers.DefaultHandler;
  7.  
  8. import bo.Book;
  9. public class Mysax extends DefaultHandler {
  10. private String tag;
  11. private Book book;
  12. private List<Book> list;
  13. public List<Book> getList()
  14. {
  15. return list;
  16. }
  17. @Override
  18. public void characters(char[] arg0,int arg1,int arg2) throws SAXException {
  19. // TODO Auto-generated method stub
  20. super.characters(arg0,arg1,arg2);
  21. if(tag!=null)
  22. {
  23. String string=new String(arg0,arg2).trim();
  24. if(tag.equals("tital"))
  25. {
  26. book.setTitle(string);
  27. }
  28. if(tag.equals("author"))
  29. {
  30. book.setAuthor(string);
  31. }
  32. if(tag.equals("price"))
  33. {
  34. book.setPrice(Double.parseDouble(string));
  35. }
  36. }
  37. }
  38.  
  39. @Override
  40. public void endDocument() throws SAXException {
  41. // TODO Auto-generated method stub
  42. super.endDocument();
  43. }
  44.  
  45. @Override
  46. public void endElement(String arg0,String arg1,String arg2)
  47. throws SAXException {
  48. // TODO Auto-generated method stub
  49. super.endElement(arg0,arg2);
  50. if(arg2.equals("book"))
  51. {
  52. list.add(book);
  53. book=null;//为下一步解析做准备
  54. }
  55. tag=null;
  56. }
  57.  
  58. @Override
  59. public void startDocument() throws SAXException {
  60. // TODO Auto-generated method stub
  61. super.startDocument();
  62. list=new ArrayList<Book>();
  63. }
  64.  
  65. @Override
  66. public void startElement(String arg0,String arg2,Attributes arg3) throws SAXException {
  67. // TODO Auto-generated method stub
  68. super.startElement(arg0,arg2,arg3);
  69. if(arg2.equals("book"))
  70. {
  71. book=new Book();
  72. String bookNo=arg3.getValue("bookno");
  73. book.setBookno(bookNo);
  74. }
  75. tag=arg2;
  76. }
  77. }
  78.  


SaxParse.java(解析主类,调用其他类,实现其解析功能)


  1. package sax;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. import javax.swing.text.html.parser.Parser;
  10. import javax.xml.parsers.ParserConfigurationException;
  11. import javax.xml.parsers.SAXParser;
  12. import javax.xml.parsers.SAXParserFactory;
  13.  
  14. import org.xml.sax.SAXException;
  15.  
  16. import bo.Book;
  17. import bo.XmlParse;
  18. public class SaxParse implements XmlParse {
  19.  
  20. public List<Book> xmlparse(String fileName) {
  21. List<Book> list=new ArrayList<Book> ();//利用集合装载对象进行解析处理
  22. SAXParserFactory factory=SAXParserFactory.newInstance();//建立Sax解析工程对象
  23. Mysax handler=null;
  24. try {
  25. SAXParser parser=factory.newSAXParser();//建立解析对象
  26. InputStream is=new FileInputStream(fileName);
  27. handler=new Mysax();
  28. parser.parse(is,handler);
  29. } catch (FileNotFoundException e) {
  30. // TODO Auto-generated catch block
  31. e.printStackTrace();
  32. } catch (ParserConfigurationException e) {
  33. // TODO Auto-generated catch block
  34. e.printStackTrace();
  35. } catch (SAXException e) {
  36. // TODO Auto-generated catch block
  37. e.printStackTrace();
  38. } catch (IOException e) {
  39. // TODO Auto-generated catch block
  40. e.printStackTrace();
  41. }
  42. list=handler.getList();
  43.  
  44. return list;
  45. }
  46. public static void main(String[] args) {
  47. // TODO Auto-generated method stub
  48. XmlParse parse=new SaxParse();
  49. List<Book> list=parse.xmlparse("book.xml");
  50. System.out.println("Sax 解析结果:");
  51. for(Book book:list)
  52. {
  53. System.out.println(book);
  54. }
  55.  
  56.  
  57. }
  58.  
  59. }

猜你在找的XML相关文章