How use XMLReaderFactory now? because this is deprecated
Asked Answered
I

3

12

I'm trying use XMLReaderFactory, but this is deprecated. Now, how Can I create a instance of XMLReader?

Illegal answered 27/6, 2018 at 22:45 Comment(0)
B
26

The documentation for XMLReaderFactory recommends SAXParserFactory

SAXParserFactory parserFactory = SAXParserFactory.newInstance();
SAXParser parser = parserFactory.newSAXParser();
XMLReader reader = parser.getXMLReader();
Bund answered 27/6, 2018 at 22:48 Comment(2)
@Igor If this post answers your question, please consider to mark the answer as "accepted". Kindly refer to What should I do when someone answers my question?Spark
Somehow this new XMLReader doesn't work with the same code as before. Maybe that's why it's not accepted. I'm reverting to the deprecated factory.Mercola
G
0

XMLReader xmlReader = org.apache.poi.util.XMLHelper.newXMLReader(); Creates a new SAX XMLReader, with sensible defaults.

Greenhaw answered 29/5, 2023 at 7:7 Comment(0)
A
0

Same issue here as we just make a big jump from Jdk8 to jdk17, for compatibility here is my code from my own factory:

XMLReader saxReader;
try {
    SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    parserFactory.setNamespaceAware(true);
    SAXParser parser = parserFactory.newSAXParser();
    XMLReader saxReader  = parser.getXMLReader();           
    saxReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
} catch (javax.xml.parsers.ParserConfigurationException parseEx) {
    throw new SAXException(parseEx);
} 

making the parser ns aware solved the compatibility issue link to the usage of a DefaultHandler. The XXE protection was already there (see: owasp XXE) but worth mentioning anyway I think.
the try catch wrapping is due to the fact that a new exception is now thrown (you can remove this part and manage exception somewhere else if you can)

Adaminah answered 13/10, 2023 at 14:47 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.