Difference SAXParserFactory XMLReaderFactory. Which one to choose?
Asked Answered
P

2

9

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

SAXParserFactory:

example code:

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

XMLReaderFactory:

example code:

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

question:

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

Which one should you choose?

Pestilent answered 14/5, 2012 at 12:40 Comment(1)
SAXParserFactory is JAXP, whereas XMLReaderFactory is the "official" SAX API. I remember the old SAX2 book from O'Reilly recommending the official API, due to the way in which it handled namespaces. I don't know if this is relevant anymore. But, I guess I would use XMLReaderFactory unless I needed JAXP for some reason. Maybe someone else can provide some more current input.Chur
C
4

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.

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

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

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

  • The Java Runtime comes with a default implementation XMLReader

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

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.

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

Corroborate answered 14/5, 2012 at 14:28 Comment(0)
T
3

Elliotte Rusty Harold, who has written a book on XML processing in Java, once wrote:

SAXParserFactory [is] a hideous, evil monstrosity of a class that should 
be hung, shot, beheaded, drawn and quartered, burned at the stake, 
buried in unconsecrated ground, dug up, cremated, and the ashes tossed 
in the Tiber while the complete cast of Wicked sings "Ding dong, the 
witch is dead."  --Elliotte Rusty Harold on xml-dev 

I wouldn't actually go that far (and if I did, I would try and make it grammatical). Like a lot of JAXP, it's not the most wonderful piece of design; but I do use it as the default way of getting a parser instance, if only because it's consistent with other parts of JAXP.

Historically, the two were developed in parallel. JAXP originally tried to add missing functionality to SAX1, but in parallel, SAX2 was coming along and filled in some of the gaps, so this particular gap got filled twice.

Tem answered 14/5, 2012 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.