java SAXParser 和 XMLReader 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13878998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between SAXParser and XMLReader
提问by sakura
What is the difference between below two snippet, if i just have to parse the XML?
如果我只需要解析 XML,以下两个片段之间有什么区别?
1.By using SAXParser parse
method:
1.通过使用SAXParserparse
方法:
SAXParserFactory sfactory = SAXParserFactory.newInstance();
SAXParser parser = sfactory.newSAXParser();
parser.parse(new File(filename), new DocHandler());
Now using XMLReader's parse
method acquired from SAXParser
现在使用parse
从 SAXParser 获取的XMLReader方法
SAXParserFactory sfactory = SAXParserFactory.newInstance();
SAXParser parser = sfactory.newSAXParser();
XMLReader xmlparser = parser.getXMLReader();
xmlparser.setContentHandler(new DocHandler());
xmlparser.parse(new InputSource("test1.xml"));
Despite of getting more flexibility, is there any other difference?
尽管获得了更多的灵活性,但还有其他区别吗?
回答by J?rn Horstmann
The parse
methods of SAXParser
just delegate to an internal instanceof XMLReader
and are usually more convenient. For some more advanced usecases you have to use XMLReader
. Some examples would be
该parse
方法SAXParser
仅仅委托给一个内部的instanceofXMLReader
和通常更方便。对于一些更高级的用例,您必须使用XMLReader
. 一些例子是
- Setting non-standard features of the implementation
- Setting different classes as
ContentHandler
,EntityResolver
orErrorHandler
- Switching handlers while parsing
- 设置实现的非标准特性
- 将不同的类设置为
ContentHandler
,EntityResolver
或ErrorHandler
- 解析时切换处理程序
回答by Evgeniy Dorofeev
As you noticed XMLReader belongs to org.xml.sax (that comes from http://www.saxproject.org/) and SAXParser to javax.xml.parsers. SAXParser internally uses XMLReader. You can work with XMLReader directly
正如您所注意到的,XMLReader 属于 org.xml.sax(来自http://www.saxproject.org/),而 SAXParser 属于 javax.xml.parsers。SAXParser 在内部使用 XMLReader。您可以直接使用 XMLReader
XMLReader xr = XMLReaderFactory.createXMLReader();
xr.setContentHandler(handler);
xr.setDTDHandler(handler);
...
but you will notice that SAXParser is more convinient to use. That is, SAXParser was added for convenience.
但是您会注意到 SAXParser 使用起来更方便。也就是说,为了方便起见,添加了 SAXParser。
回答by Raghunandan
http://docs.oracle.com/javase/1.5.0/docs/api/org/xml/sax/XMLReader.html. take a look at the link for XML reader and have a look athe following link for sax parser. http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/parsers/SAXParser.html.
http://docs.oracle.com/javase/1.5.0/docs/api/org/xml/sax/XMLReader.html。查看 XML 阅读器的链接,并查看以下 sax 解析器的链接。http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/parsers/SAXParser.html。
Using different parser's depends on what you want your parser to do. If your modifying, deleting contents in xml you can use W3C Dom parser. If you just want to parse and get elements you can use SAX parser. There are a few out there. So it really depends on what you want.
使用不同的解析器取决于您希望解析器做什么。如果您修改、删除 xml 中的内容,您可以使用 W3C Dom 解析器。如果您只想解析和获取元素,您可以使用 SAX 解析器。有几个。所以这真的取决于你想要什么。