日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

使用SaxParser和完整代码进行XML解析

發布時間:2023/12/3 asp.net 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用SaxParser和完整代码进行XML解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SAX解析器使用回調函數(org.xml.sax.helpers.DefaultHandler)通知客戶端XML文檔結構。 您應該擴展DefaultHandler并重寫一些方法來實現xml解析。

覆蓋的方法是

  • startDocument()和endDocument()–在XML文檔的開頭和結尾處調用的方法。
  • startElement()和endElement()–在文檔元素的開頭和結尾處調用的方法。
  • character()–以XML文檔元素的開始和結束標記之間的文本內容調用的方法。

下面的示例演示使用DefaultHandler解析和XML文檔。 它執行xml到模型類的映射并生成對象列表。

XML文檔樣本:

<?xml version="1.0" encoding="UTF-8"?> <catalog><book id="001" lang="ENG"><isbn>23-34-42-3</isbn><regDate>1990-05-24</regDate><title>Operating Systems</title><publisher country="USA">Pearson</publisher><price>400</price><authors><author>Ganesh Tiwari</author></authors></book><book id="002"><isbn>24-300-042-3</isbn><regDate>1995-05-12</regDate><title>Distributed Systems</title><publisher country="Nepal">Ekata</publisher><price>500</price><authors><author>Mahesh Poudel</author><author>Bikram Adhikari</author><author>Ramesh Poudel</author></authors></book> </catalog>

Book對象的模型類,用于將xml映射到對象

/*** Book class stores book information, after parsing the xml* @author Ganesh Tiwari*/ public class Book {String lang;String title;String id;String isbn;Date regDate;String publisher;int price;List<String> authors;public Book(){authors=new ArrayList<String>();}//getters and setters }

XML解析(Sax)的Java代碼:

import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; 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 MySaxParser extends DefaultHandler {List<Book> bookL;String bookXmlFileName;String tmpValue;Book bookTmp;SimpleDateFormat sdf= new SimpleDateFormat("yy-MM-dd");public MySaxParser(String bookXmlFileName) {this.bookXmlFileName = bookXmlFileName;bookL = new ArrayList<Book>();parseDocument();printDatas();}private void parseDocument() {// parseSAXParserFactory factory = SAXParserFactory.newInstance();try {SAXParser parser = factory.newSAXParser();parser.parse(bookXmlFileName, this);} catch (ParserConfigurationException e) {System.out.println("ParserConfig error");} catch (SAXException e) {System.out.println("SAXException : xml not well formed");} catch (IOException e) {System.out.println("IO error");}}private void printDatas() {// System.out.println(bookL.size());for (Book tmpB : bookL) {System.out.println(tmpB.toString());}}@Overridepublic void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException {// if current element is book , create new book// clear tmpValue on start of elementif (elementName.equalsIgnoreCase("book")) {bookTmp = new Book();bookTmp.setId(attributes.getValue("id"));bookTmp.setLang(attributes.getValue("lang"));}// if current element is publisherif (elementName.equalsIgnoreCase("publisher")) {bookTmp.setPublisher(attributes.getValue("country"));}}@Overridepublic void endElement(String s, String s1, String element) throws SAXException {// if end of book element add to listif (element.equals("book")) {bookL.add(bookTmp);}if (element.equalsIgnoreCase("isbn")) {bookTmp.setIsbn(tmpValue);}if (element.equalsIgnoreCase("title")) {bookTmp.setTitle(tmpValue);}if(element.equalsIgnoreCase("author")){bookTmp.getAuthors().add(tmpValue);}if(element.equalsIgnoreCase("price")){bookTmp.setPrice(Integer.parseInt(tmpValue));}if(element.equalsIgnoreCase("regDate")){try {bookTmp.setRegDate(sdf.parse(tmpValue));} catch (ParseException e) {System.out.println("date parsing error");}}}@Overridepublic void characters(char[] ac, int i, int j) throws SAXException {tmpValue = new String(ac, i, j);}public static void main(String[] args) {new MySaxParser("catalog.xml");} }

解析輸出:

Book [lang=ENG, title=Operating Systems, id=001, isbn=23-34-42-3, regDate=Thu May 24 00:00:00 NPT 1990, publisher=USA, price=400, authors=[Ganesh Tiwari]] Book [lang=null, title=Distributed Systems, id=002, isbn=24-300-042-3, regDate=Fri May 12 00:00:00 NPT 1995, publisher=Nepal, price=500, authors=[Mahesh Poudel, Bikram Adhikari, Ramesh Poudel]]

參考:在GT's Blog上, 使用我們的JCG合作伙伴 Ganesh Tiwari 提供的完整代碼 , 使用SaxParser進行XML解析 。


翻譯自: https://www.javacodegeeks.com/2012/01/xml-parsing-using-saxparser-with.html

總結

以上是生活随笔為你收集整理的使用SaxParser和完整代码进行XML解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。