区别SAXParserFactory XMLReaderFactory.选择哪一个? [英] Difference SAXParserFactory XMLReaderFactory. Which one to choose?

查看:365
本文介绍了区别SAXParserFactory XMLReaderFactory.选择哪一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它们两者似乎都具有相同的目的(创建XMLReader). 有些教程包含一个,而另一些则包含.

Both of them seem to have the same purpose (create a XMLReader). Some Tutorials contain the one, some the other.

SAXParserFactory:

  • http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/SAXParserFactory.html
  • seems to be more configurable
  • more boiler-plate code
  • officially supported api

示例代码:

// SAXParserFactory
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.parse(new InputSource("document.xml"));

XMLReaderFactory:

  • http://docs.oracle.com/javase/7/docs/api/org/xml/sax/helpers/XMLReaderFactory.html
  • two lines less code
  • less configurable
  • comunity supported and comes with no waranty

示例代码:

// XMLReaderFactory
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.parse(new InputSource("document.xml"));

问题:

这些是主要区别还是我监督了一些?

Are these the main differences or are there some i've overseen.

您应该选择哪个?

推荐答案

JAXP APIsjavax.xml.parsers包中定义.该软件包包含与供应商无关的工厂类,例如SAXParserFactory,为您提供了S AXParser.

The main JAXP APIs are defined in the javax.xml.parsers package. That package contains vendor-neutral factory classes like the SAXParserFactory which give you a SAXParser.

SAXParserFactory定义了一个工厂API,使应用程序可以配置和获取基于SAX的解析器来解析XML文档.

The SAXParserFactory defines a factory API that enables applications to configure and obtain a SAX based parser to parse XML documents.

  • SAXParser定义包装XMLReader的API 实现类.

  • The SAXParser defines the API that wraps an XMLReader implementation class.

软件包org.xml.sax定义了基本的SAX API.

The Package org.xml.sax defines the basic SAX APIs.

Java运行时带有默认实现XMLReader

The Java Runtime comes with a default implementation XMLReader

SAXParserFactory向您隐藏(SAX1)ParserFactory/(SAX2)XMLReaderFactory ...的详细信息.

The SAXParserFactory hides details of which (SAX1) ParserFactory / (SAX2) XMLReaderFactory, ... from you.

如果您希望能够用其他JAXP解析器(默认实现中可能存在已知的不兼容/错误)替换默认的JAXP解析器,则应使用供应商中性的SAXParserFactory.

If you want to be able to replace the default JAXP Parser by a different JAXP Parser (there may be a known incomapatibilty / bug in the default implementation) implementation you should use the vendor neutral SAXParserFactory.

如果您知道您的应用程序将始终使用SAX2 XMLReader,则可以使用XMLReaderFactory.

If you know that your application will allways use a SAX2 XMLReader you may use the XMLReaderFactory.

这篇关于区别SAXParserFactory XMLReaderFactory.选择哪一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆