無(wú)為

          無(wú)為則可為,無(wú)為則至深!

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            190 Posts :: 291 Stories :: 258 Comments :: 0 Trackbacks

          公告

          與有奉獻(xiàn)精神和分享精神的人做朋友!

          Locations of visitors to this page
          Subscribe by Anothr

          搜索

          •  

          積分與排名

          • 積分 - 582364
          • 排名 - 83

          最新評(píng)論

          閱讀排行榜

          隨著互聯(lián)網(wǎng)技術(shù)的發(fā)展,XML技術(shù)變得越來(lái)越重要,從而出現(xiàn)了很多
          與XML的整合應(yīng)用方面的技術(shù),SAX就是其中之一。在這里,我就簡(jiǎn)要
          談?wù)刯ava怎樣使用sax對(duì)xml文檔的解析。

          首先,讓我們來(lái)談?wù)凷AX對(duì)XML文檔的解析機(jī)制。一般情況下,SAX 處理器
          分析XML代碼將生成以下事件:


          Start document
          Start element ()
          Characters (white space)
          Start element ()
          Characters ()
          End element ()
          ............
          End element ()

          具體解析步驟如下
          一、 創(chuàng)建事件處理程序,SAX 提供應(yīng)用程序可擴(kuò)展的類(lèi) DefaultHandler。
          創(chuàng)建的解析類(lèi)config必須繼承類(lèi) DefaultHandler。
          如:public class Config extends DefaultHandler{}
          二、使用 JAXP 來(lái)創(chuàng)建解析器:使用 JAXP 中的類(lèi) XMLReaderFactory
          來(lái)創(chuàng)建解析器。首先聲明 XMLReader的一個(gè)對(duì)象 parser。然后使用
          XMLReaderFactory的createXMLReader方法來(lái)創(chuàng)建 SAXParser。
          XMLReader parser =
          XMLReaderFactory.createXMLReader(
          "org.apache.xerces.parsers.SAXParser");
          三、一旦創(chuàng)建了解析器,則需要將 config 設(shè)置為內(nèi)容處理程序,
          以便于其接收事件。
          parser.setContentHandler(this);

          但是,當(dāng)然總會(huì)有可能在試圖進(jìn)行解析時(shí),數(shù)據(jù)有問(wèn)題。這時(shí)應(yīng)該創(chuàng)建
          ErrorHandler 在這樣的情況下,有一個(gè)處理程序來(lái)處理錯(cuò)誤和內(nèi)容。
          parser.setErrorHandler(this);
          四、準(zhǔn)備對(duì)文件進(jìn)行實(shí)際解析。應(yīng)用程序文件傳遞給 parse(),然后應(yīng)用程序
          會(huì)繼續(xù)運(yùn)行。
          parser.parse(confFile);
          五、接下來(lái)就開(kāi)始對(duì)文檔進(jìn)行解析了。前面說(shuō)過(guò)SAX的處理機(jī)制,SAX在處理XML流
          的過(guò)程中,首先產(chǎn)生startDocument()事件,實(shí)際上該事件什么也沒(méi)有發(fā)生,因?yàn)?br />還沒(méi)有定義任何解析的事件。使用 startDocument() 事件只是為了通知文檔的開(kāi)
          始。類(lèi)似其它 SAX 事件,該事件拋出 SAXException。
          例如:
          public void startDocument()
          throws SAXException {
          System.out.println(
          "Tallying survey results...");
          }

          六、跟下來(lái),就正式開(kāi)始對(duì)文件進(jìn)行解析了。這時(shí)SAX會(huì)產(chǎn)生一個(gè)startElement()
          事件,對(duì)于每個(gè)元素,都會(huì)回送一個(gè)傳遞給 startElement() 事件的名稱(chēng)。不過(guò)
          解析器實(shí)際所傳遞是:該元素的名稱(chēng)空間信息;該元素的實(shí)際名稱(chēng)或 localName;
          名稱(chēng)空間別名和 localName 的組合(否則是限定名或 qname);以及該元素任何
          屬性。startElement() 事件還提供對(duì)元素屬性的訪問(wèn)。將這些屬性傳遞進(jìn)稱(chēng)為
          Attributes的數(shù)據(jù)結(jié)構(gòu)。根據(jù)屬性在數(shù)組中位置或?qū)傩缘拿Q(chēng),可以檢索該屬性值。
          例如:
          public void startElement(
          String namespaceURI,
          String localName,
          String qName,
          Attributes atts)
          throws SAXException {
          String s = (!"".equals(localName)) ? localName : qName;

          System.out.print("Start element: ");
          System.out.println(localName);
          tag = s;
          if (localName == "Connector") {
          System.out.println("name: "
          + atts.getValue("name"));
          } else if (localName == "port") {
          thisQuestion = atts.getValue("servername");
          }else if (localName == "maxThreadPoolSize") {
          thisQuestion = atts.getValue("poolname");
          }
          if (localName == "Database") {
          System.out.println("name: "
          + atts.getValue("name"));
          } else if (localName == "driverName") {
          thisQuestion = atts.getValue("dataname");
          }else if (localName == "encoding") {
          thisQuestion = atts.getValue("codename");
          }
          thisElement = localName;


          for (int att = 0;att < atts.getLength(); att++) {
          String attName = atts.getLocalName(att);
          System.out.println(" "
          + attName + ": "
          + atts.getValue(attName));
          }


          }
          七、有了元素,接下來(lái)用 characters() 來(lái)檢索實(shí)際的數(shù)據(jù)。這時(shí)可以使用下面的
          方法獲得實(shí)際的數(shù)據(jù):
          可以用String s = new String(ch, start, length).trim();
          這個(gè)方法來(lái)獲得數(shù)據(jù)。
          例如:
          public void characters(char[] ch,
          int start,
          int length)
          throws SAXException {

          String s = new String(ch, start, length).trim();
          if (!"".equals(s)) {
          if ("port".equals(tag)) {
          System.out.println(Integer.parseInt(s));
          } else if ("maxThreadPoolSize".equals(tag)) {
          System.out.println(Integer.parseInt(s));
          }
          //System.out.println(tag+" = "+s);
          }

          s = new String(ch, start, length).trim();
          if (!"".equals(s)) {
          if ("driverName".equals(tag)) {
          System.out.println(s);
          } else if ("encoding".equals(tag)) {
          System.out.println(s);
          }
          //System.out.println(tag+" = "+s);
          }
          }
          八、接下來(lái)的要做的就是用endElement()來(lái)結(jié)束對(duì)一個(gè)xml的 element的解析。
          例如:下面的方法
          public void endElement(String namespaceURI,
          String localName,
          String qName)
          throws SAXException {

          printIndent(indent);
          System.out.println("End Element: "+localName);
          indent = indent - 4;
          thisQuestion = "";
          thisElement = "";
          九、最后就是告訴程序,xml文件解析完畢,此時(shí),用endDocument()來(lái)結(jié)束程序。
          例如:
          public void endDocument() {}

          總的來(lái)說(shuō),sax處理xml的方法還基本上是差不多的,只是一二步的方法相對(duì)多一點(diǎn),
          那些其他的方法我這里就不多說(shuō)了,由讀者自己去查找資料。



          凡是有該標(biāo)志的文章,都是該blog博主Caoer(草兒)原創(chuàng),凡是索引、收藏
          、轉(zhuǎn)載請(qǐng)注明來(lái)處和原文作者。非常感謝。

          posted on 2006-05-21 17:10 草兒 閱讀(568) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): ajax
          主站蜘蛛池模板: 宜兴市| 英吉沙县| 乌审旗| 吴江市| 安陆市| 台州市| 耿马| 阜新市| 西林县| 阿克苏市| 蓝山县| 镇远县| 赤峰市| 当阳市| 剑河县| 奉节县| 五指山市| 都匀市| 清涧县| 若羌县| 夹江县| 新田县| 喀喇沁旗| 阳原县| 白朗县| 阳朔县| 新野县| 道真| 梓潼县| 绥滨县| 深水埗区| 庄浪县| 鄄城县| 巴楚县| 泰州市| 鄂温| 广饶县| 龙山县| 布拖县| 临汾市| 潜江市|