I'm trying use XMLReaderFactory, but this is deprecated. Now, how Can I create a instance of XMLReader?
How use XMLReaderFactory now? because this is deprecated
The documentation for XMLReaderFactory
recommends SAXParserFactory
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
SAXParser parser = parserFactory.newSAXParser();
XMLReader reader = parser.getXMLReader();
@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
XMLReader xmlReader = org.apache.poi.util.XMLHelper.newXMLReader();
Creates a new SAX XMLReader, with sensible defaults.
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)
© 2022 - 2025 — McMap. All rights reserved.