以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DOM/SAX/XPath 』  (http://bbs.xml.org.cn/list.asp?boardid=11)
----  SAX如下两种用法有什么区别和一些相关介绍  (http://bbs.xml.org.cn/dispbbs.asp?boardid=11&rootid=&id=20779)


--  作者:flyfoxs
--  发布时间:7/29/2005 10:55:00 PM

--  SAX如下两种用法有什么区别和一些相关介绍
方法1)用类 XMLReaderFactory
示例

      XMLReader parser =
         XMLReaderFactory.createXMLReader(PARSER_NAME);
      parser.setContentHandler(bestDeal.getContentHandler());
      parser.parse(args[0]);

方法2)用类 SAXParserFactory

示例
static public void main(String[] args)
{
 String filename = null;
 boolean validation = false;
 filename="9-21.xml";
 SAXParserFactory spf = SAXParserFactory.newInstance();
 SAXParser saxParser=null;
 try {
  saxParser = spf.newSAXParser();
 } catch (Exception ex) {
                 System.err.println(ex);
  System.exit(1);
 }
 try {
 saxParser.parse(new File(filename),new SAXReader());
 } catch (SAXException se) {
  System.err.println(se.getMessage());
                 System.exit(1);
                }

并且我发现这两个类在的包也不一样.分别是org.xml.sax 和 javax.xml.parsers
!


希望路过的高手指点一二!


--  作者:wtfly
--  发布时间:8/24/2005 9:40:00 AM

--  
好象一个是XERCES,一个是JAXP
--  作者:flyfoxs
--  发布时间:1/18/2006 7:49:00 PM

--  
前几天收到一个网友问我这个问题,我才记起我在这个地方有一个脚印。

楼上其实说的很对,实际上jaxp只是使用了提供的sax(apache),进行了重新的封装,也就是实际上是jaxp来调用xerces。

在jaxp(第二个例子)里面最后使用SAXParser 来解析,读取数据吗?

我把它的文档挑了一点出来。

public abstract class SAXParser
extends java.lang.Object
Defines the API that wraps an XMLReader implementation class. In JAXP 1.0, this class wrapped the Parser interface, however this interface was replaced by the XMLReader. For ease of transition, this class continues to support the same name and interface as well as supporting new methods. An instance of this class can be obtained from the SAXParserFactory.newSAXParser() method. Once an instance of this class is obtained, XML can be parsed from a variety of input sources. These input sources are InputStreams, Files, URLs, and SAX InputSources.


也就是说最后SAXParser还是调用xerces里面的XMLReader来完成操作。

参考:http://people.apache.org/~edwingo/jaxp-ri-1.2.0-fcs/docs/api/javax/xml/parsers/SAXParser.html
   [URL=http://webmail.vh.com.tw/Microsoft/%B8%F3%A5%AD%A5xe%A4%C6%B5{%A6%A1%B3]%ADp-XML-Java-SOAP%BE%E3%A6X%C0%B3%A5%CE/3.htm]XML解析器[/URL]

[此贴子已经被作者于2006-1-18 20:16:48编辑过]

--  作者:flyfoxs
--  发布时间:1/23/2006 7:16:00 PM

--  
今天又看到正面的这个文章,对上面的问题很对口

http://blog.itfans.name/?p=35


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
7,019.531ms