Class XMLParser
Object
XMLParser
Utility functions for parsing XML files using standard Java APIs.
- Author:
- Owen Feehan
-
Method Summary
Modifier and TypeMethodDescriptionstatic DocumentBuilderCreates a document-builder with default error-handling.static DocumentCreates a new XML document.static TransformerFactoryCreates aTransformerFactorythat ignores any DTDs in the document and disables external entities (to prevent XXE attacks).static DocumentParses a XML file to produce aDocument.
-
Method Details
-
createBuilderWithDefaultErrorHandler
public static DocumentBuilder createBuilderWithDefaultErrorHandler() throws ParserConfigurationExceptionCreates a document-builder with default error-handling.- Returns:
- a document-builder with default-error-handling.
- Throws:
ParserConfigurationException- if aDocumentBuildercannot be created which satisfies the configuration requested.
-
createTransformerFactory
public static TransformerFactory createTransformerFactory() throws TransformerConfigurationExceptionCreates aTransformerFactorythat ignores any DTDs in the document and disables external entities (to prevent XXE attacks).- Returns:
- a newly created
DocumentBuilderFactory - Throws:
TransformerConfigurationException- if thrown byTransformerFactory.setFeature(java.lang.String, boolean).
-
parse
public static Document parse(File file) throws SAXException, IOException, ParserConfigurationException Parses a XML file to produce aDocument.- Parameters:
file- the XML file to parse.- Returns:
- the parsed document
- Throws:
SAXException- if any parse errors occur.IOException- if any IO errors occur.ParserConfigurationException- if aDocumentBuildercannot be created which satisfies the configuration requested.
-
createNewDocument
Creates a new XML document.- Returns:
- the newly created
Document - Throws:
ParserConfigurationException- if aDocumentBuildercannot be created which satisfies the configuration requested.
-