人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統下載站 > 技術開發教程 > 詳細頁面

解析器的運用(JAXP)

解析器的運用(JAXP)

更新時間:2022-10-14 文章作者:未知 信息來源:網絡 閱讀次數:

分析器的使用(一)

作者: 常明 寫作日期:2000-4-26



簡介
這組文章主要就目前有的XML分析器分別介紹一下他們的接口和用法。
其中包括SUN的XML分析器,IBM的分析器XML4J,XML4C,ORACLE的分析器,EXPAT。
SUN的JAXP
JAXP API全部包括在JAXP.JAR中,它有兩個工廠類 SAXParserFactory和DocumentBuilderFactory 分別對應SAX接口和DOM接口。
這兩個工廠類的作用是,通過設定其中的屬性創建合適的分析器,它可以配合各種分析器的具體實現一起使用。這就是說SAXParserFactor可以和任何符合SAX接口的PARSER一起用,不一定就是SUN的分析器;DocumentBuidlerFactory也一樣,不一定用SUN的實現。對于sax只要parser是實現org.xml.sax.Parser接口的,對于DOM就是文檔對象實現了 org.w3c.dom.*的接口。
SUN的分析器的實現是com.sun.xml.parser,它是實現了sax接口的,它的DOM實現則是基于SAX的,所以他們的分析器是一個。
下面介紹DOM的編程,首先要引入以下JAXP API定義:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.DocumentBuilder;
如果要處理SAX異常加入:
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
文件I/O:
import java.io.File;
import java.io.IOException;
如果要處理DOM異常加入:
import org.w3c.dom.Document;
import org.w3c.dom.DOMException;
程序如下,聲明一個靜態全局的DOM的Document,方便下面的處理,Document可以從文件裝入,寫可以通過程序寫入,否則是空的。
public class DomEcho
{
static Document document;

public static void main (String argv [])
{
...
}

在分析過程中的異常處理的程序如下,這里有四種異常的處理:
public static void main (String argv [])
{
if (argv.length != 1) {
...
}

try {

} catch (SAXParseException spe) {
 // Error generated by the parser
 System.out.println ("\n** Parsing error"
+ ", line " + spe.getLineNumber ()
+ ", uri " + spe.getSystemId ());
 System.out.println(" " + spe.getMessage() );

 // Use the contained exception, if any
 Exceptionx = spe;
 if (spe.getException() != null)
 x = spe.getException();
 x.printStackTrace();

} catch (SAXException sxe) {
 // Error generated by this application
 // (or a parser-initialization error)
 Exceptionx = sxe;
 if (sxe.getException() != null)
 x = sxe.getException();
 x.printStackTrace();

} catch (ParserConfigurationException pce) {
 // Parser with specified options //can t be built
 pce.printStackTrace();

} catch (IOException ioe) {
 // I/O error
 ioe.printStackTrace();
}

}// main
生成分析器,有兩步,首先實例化工廠,再由工廠生成DocumentBuilder,最后開始分析:
DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
try {
 DocumentBuilder builder =factory.newDocumentBuilder();
 document = builder.parse( new File(argv[0]) );

} catch (SAXParseException spe) {

有了Document對象后就可以做各種DOM操作,以下程序輸出XML:
XmlDocument xdoc = (XmlDocument) document;
xdoc.write (System.out);

如果要使用SAX接口程序就稍復雜一些,首先引入定義:
import java.io.*;
import org.xml.sax.*;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;


和DOM的編程相似,首先一樣實例化DocumentBuilderFactory,創建一個SAXParser,然后開始分析,期間處理各種異常:
public static void main (String argv [])

{
if (argv.length != 1) {
System.err.println ("Usage: cmd filename");
System.exit (1);
}

// Use the default (non-validating) parser
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
 NoAction noaction;
// Parse the input
SAXParser saxParser =factory.newSAXParser();
saxParser.setDocumentHandler(noaction);
saxParser.parse(new File(argv [0]), new Echo() );

} catch (Throwable t) {
t.printStackTrace ();
}
System.exit (0);
}
但用SAX是通過處理事件來進行的,所以在分析之前,一般要設定處理器,使用saxparser的SetDocumentHandler方法來設定。 下面是一個什么都不做的事件處理器,它實現了DocumentHandler接口。
class NoAction implement DocumentHandler{
... 
 public void startDocument ()
throws SAXException
{
}

public void endDocument ()
throws SAXException
{
}

public void startElement (String name, AttributeList attrs)
throws SAXException
{
}

public void endElement (String name)
throws SAXException
{
}

public void characters (char buf [], int offset, int len)
throws SAXException
{
}
 ...
以上就基本介紹了sun的xml分析器的使用,有任何問題請發mail給我們一起討論。

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

網站地圖xml | 網站地圖html
主站蜘蛛池模板: 禹城市| 新郑市| 保靖县| 乐平市| 益阳市| 错那县| 高雄市| 老河口市| 垫江县| 郯城县| 丰县| 多伦县| 桑日县| 贵州省| 松阳县| 江油市| 北安市| 万宁市| 新沂市| 赤城县| 太谷县| 远安县| 华池县| 神池县| 衡山县| 开远市| 吐鲁番市| 凌源市| 虎林市| 建德市| 云梦县| 寿光市| 武陟县| 会同县| 宜阳县| 肥东县| 白河县| 资兴市| 敖汉旗| 岚皋县| 元氏县|