<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 20,  comments - 7,  trackbacks - 0

    All about JAXP

    http://www-128.ibm.com/developerworks/java/library/x-jaxp

    Brett McLaughlin

    ?

    Document Object Model (DOM)

    Simple API for XML (SAX)

    ?

    JDK1.4

    javax.xml.parsers
    javax.xml.transform
    javax.xml.transform.dom
    javax.xml.transform.sax
    javax.xml.transform.stream

    ?

    JDK5.0

    javax.xml
    javax.xml.datatype
    javax.xml.namespace
    javax.xml.parsers
    javax.xml.transform
    javax.xml.transform.dom
    javax.xml.transform.sax
    javax.xml.transform.stream
    javax.xml.validation
    javax.xml.xpath

    ?

    ?

    嚴格來說, JAXP 并沒有提供新的解析 XML 的方法,但是它使得我們更容易地使用 DOM 或者 SAX 來進行解析任務,更能以一種 vendor-neutral 的方式來使用 DOM SAX

    ?

    JAXP SAX, DOM, JDOM dom4j (這四個都可以解析 XML )沒有可比較性,它并沒有提供一個新的解析 XML 的方法。

    ?

    JAXP API, located in the javax.xml.parsers package. All of these classes sit on top of an existing parser. 其中的 6 個類都是建立在已有的解析上面的。

    ?

    JDOM dom4j 都提供不同的模型來接受來自 SAX/DOM 的數據,他們從內部來講都是使用了 SAX ,不過是做了些修改。

    ?

    此外, Java 1.5 Xerces 的包 org.apache.xerces 被放到了 com.sun.org.apache.xerces.internal

    ?

    First, com.sun.xml.tree.XMLDocument class is not part of JAXP. It is part of Sun's Crimson parser, packaged in earlier versions of JAXP.

    ?

    Second, a major purpose of JAXP is to provide vendor independence when dealing with parsers. With JAXP, you can use the same code with Sun's XML parser, Apache's Xerces XML parser, and Oracle's XML parser.

    ?

    先從 SAX 說起,我們只用繼承 DefaultHandler (org.xml.sax.helpers 包中 ) 就能獲得所有的 callbacks ,只用在需要的方法中加入實現的代碼。

    ?

    Here's the typical SAX routine:

    1 Create a SAXParser instance using a specific vendor's parser implementation.

    2 Register callback implementations (by using a class that extends DefaultHandler, for example).

    3 Start parsing and sit back as your callback implementations are fired off.

    ?

    SAX 必須 指定 XML 驅動(如 org.apache.xerces.parsers.SAXParser ),而 JAXP 提供了更好的選擇,只要我們提供什么 XML 驅動(在 classpath 中配置),它就調用什么驅動,不需要改動代碼。

    ? ?

    new SAXParserFactory.newSAXParser() 就返回 JAXP SAXParser 類,這個類包裝了 SAX parser (an instance of the SAX class org.xml.sax.XMLReader )

    ?

    In Listing 1, you can see that two JAXP-specific problems can occur in using the factory: the inability to obtain or configure a SAX factory, and the inability to configure a SAX parser.

    The first of these problems, represented by a FactoryConfigurationError, usually occurs when the parser specified in a JAXP implementation or system property cannot be obtained.

    The second problem, represented by a ParserConfigurationException, occurs when a requested feature is not available in the parser being used. Both are easy to deal with and shouldn't pose any difficulty when using JAXP.

    ?

    SAXParser parse 方法可以接受 SAX InputSource Java InputStream 或者 URL in String form

    ?

    可以通過 SAXParser getXMLReader() 方法來獲得底層的 SAX parser org.xml.sax.XMLReader 的實例),這樣就可以使用各個 SAXParser 方法。 [ 參照 Listing2]

    ?? ?

    ?

    使用 DOM??

    The only difference between DOM and SAX in this respect is that with DOM you substitute DocumentBuilderFactory for SAXParserFactory, and DocumentBuilder for SAXParser.

    ?

    The major difference is that variations of the parse() method do not take an instance of the SAX DefaultHandler class. Instead they return a DOM Document instance representing the XML document that was parsed. The only other difference is that two methods are provided for SAX-like functionality:

    • setErrorHandler(), which takes a SAX ErrorHandler implementation to handle problems that might arise in parsing
    • setEntityResolver(), which takes a SAX EntityResolver implementation to handle entity resolution

    JAXP 的使用

    1. Source for input

    The javax.xml.transform.Source interface is the basis for all input into JAXP and the transformation API. This interface defines only two methods -- getSystemId() and setSystemId(String systemId) .

    ?

    ?

    JAXP 提供了三個實現 Source 接口的類:

    • javax.xml.transform.dom.DOMSource passes a DOM Node (and its children) into JAXP.
    • javax.xml.transform.sax.SAXSource passes the results of SAX callbacks (from an XMLReader ) into JAXP.
    • javax.xml.transform.stream.StreamSource passes XML wrapped in a File , InputStream , or Reader into JAXP.? ?

    2. Result for output

    ? javax.xml.transform.Result 也有兩個方法: getSystemId() setSystemId(String systemId) 同樣有三個實現類:

    • javax.xml.transform.dom.DOMResult passes transformed content into a DOM Node .
    • javax.xml.transform.sax.SAXResult passes the results of a transformation to a SAX ContentHandler .
    • javax.xml.transform.stream.StreamResult passes the transformed *ML into a File , OutputStream , or Writer .

    3. Performing transformations with JAXP

    1)Getting a Factory? ???

    2)Creating a Transformer? ?
    3)Performing the transformation?? ?
    4)Caching XSL stylesheets

    JAXP this way has two significant limitations:

    • The Transformer object processes the XSL stylesheet each and every time transform() is executed.
    • Instances of Transformer are not thread-safe. You can't use the same instances across multiple threads.

    Transformer 實例不是線程安全的,不能通過多線程去使用同一個 Transformer 實例。

    5)Loading a Template

    javax.xml.transform.Templates .

    ?

    The Templates interface is thread-safe (addressing the second limitation) and represents a compiled stylesheet (addressing the first limitation).

    Templates 實例是線程安全的,可以處理一堆 XSL ,解決了上述兩個限制。

    6)From Transformer to Templates

    如果只要對一個 stylesheet 進行一個 transformation ,那么用 Transformer 比較快,沒有必要選擇 Templates 對象。但是考慮到線程安全問題,還是推薦使用 Templates

    7)Changing the XSL processor

    JAXP 默認使用 Xalan-J ,如果要使用其它 parser ,可以通過 javax.xml.transform.TransformerFactory 修改。


    java -D javax.xml.transform.TransformerFactory=[transformer.impl.class] TestTransformations?
    simple.xml simple.xsl

    ?

    posted on 2006-09-08 13:15 Lizzie 閱讀(546) 評論(1)  編輯  收藏 所屬分類: 專業積木

    FeedBack:
    # re: All about JAXP閱讀筆記
    2007-08-12 17:16 | dreamstone
    寫的不錯  回復  更多評論
      

    <2007年8月>
    2930311234
    567891011
    12131415161718
    19202122232425
    2627282930311
    2345678

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    文章分類

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产资源免费观看| 免费人妻无码不卡中文字幕系 | 亚洲人成网77777色在线播放| 亚洲一日韩欧美中文字幕在线| 亚洲视频在线观看免费| 亚洲AV无码日韩AV无码导航| a免费毛片在线播放| 亚洲无码精品浪潮| 99精品视频在线观看免费| 日日噜噜噜噜夜夜爽亚洲精品| 一本到卡二卡三卡免费高| 亚洲免费日韩无码系列| 成av免费大片黄在线观看| 国产V亚洲V天堂无码久久久| 成人自慰女黄网站免费大全| 久久久亚洲精品国产| 麻豆成人久久精品二区三区免费 | 不卡一卡二卡三亚洲| 久久久久久噜噜精品免费直播| 国产亚洲精品成人AA片新蒲金 | 在线观看亚洲免费| 免费人成在线观看视频播放| 皇色在线免费视频| 亚洲成色在线影院| 国产va精品免费观看| 国产偷国产偷亚洲高清人| 亚洲综合AV在线在线播放| 日韩人妻无码精品久久免费一| 亚洲a级成人片在线观看| 大学生美女毛片免费视频| 一进一出60分钟免费视频| 亚洲精品综合一二三区在线 | 久久精品国产亚洲av天美18| 亚洲精品无码成人片在线观看| 成人性生交大片免费看好| 亚洲一级毛片免费观看| 全部免费国产潢色一级| 久久中文字幕免费视频| 亚洲综合国产成人丁香五月激情| 亚洲精品老司机在线观看| 最近中文字幕免费mv在线视频|