Class XMLParser
Object
XMLParser
Utility functions for parsing XML files using standard Java APIs.
- Author:
- Owen Feehan
-
Method Summary
Modifier and TypeMethodDescriptionstatic DocumentBuilder
Creates a document-builder with default error-handling.static Document
Creates a new XML document.static TransformerFactory
Creates aTransformerFactory
that ignores any DTDs in the document and disables external entities (to prevent XXE attacks).static Document
Parses 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 aDocumentBuilder
cannot be created which satisfies the configuration requested.
-
createTransformerFactory
public static TransformerFactory createTransformerFactory() throws TransformerConfigurationExceptionCreates aTransformerFactory
that 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 aDocumentBuilder
cannot be created which satisfies the configuration requested.
-
createNewDocument
Creates a new XML document.- Returns:
- the newly created
Document
- Throws:
ParserConfigurationException
- if aDocumentBuilder
cannot be created which satisfies the configuration requested.
-