Java Utililty Methods XML Document Parse

List of utility methods to do XML Document Parse

Description

The list of methods to do XML Document Parse are organized into topic(s).

Method

DocumentparseDocument(InputStream is)
Parses the document.
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
return builder.parse(is);
DocumentparseDocument(InputStream is)
Parses the given input stream as XML and returns the corresponding DOM document.
getImplementation();
DOMImplementationLS implLS = (DOMImplementationLS) impl;
LSInput input = implLS.createLSInput();
input.setByteStream(is);
LSParser builder = implLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
DOMConfiguration config = builder.getDomConfig();
config.setParameter("comments", false);
config.setParameter("element-content-whitespace", false);
...
DocumentparseDocument(Path xmlPath)
parse Document
File fXmlFile = new File(xmlPath.toUri());
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
return dBuilder.parse(fXmlFile);
DocumentparseDocument(Reader reader)
parse Document
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
try {
    return factory.newDocumentBuilder().parse(new InputSource(reader));
} catch (ParserConfigurationException | SAXException e) {
    throw new IOException(e);
DocumentparseDocument(String docText)
A wrapper function to parse XML represented by a string.
DocumentBuilder dbld = DocumentBuilderFactory.newInstance().newDocumentBuilder();
return dbld.parse(new InputSource(new StringReader(docText)));
DocumentparseDocument(String documentString)
parse Document
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
StringReader stringReader = new StringReader(documentString);
InputSource inputSource = new InputSource(stringReader);
return domBuilder.parse(inputSource);
DocumentparseDocument(String documentString)
Parses the given string to a DOM object.
try {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
    StringReader stringReader = new StringReader(documentString);
    InputSource inputSource = new InputSource(stringReader);
    return domBuilder.parse(inputSource);
} catch (IOException e) {
...
DocumentparseDocument(String path)
Parses document from specified location and returns it.
try {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    return documentBuilderFactory.newDocumentBuilder().parse(new File(path));
} catch (ParserConfigurationException | IOException | SAXException e) {
    throw new RuntimeException(e);
DocumentparseDocument(String xmlString)
parse Document
DocumentBuilder builder = builderFactory.newDocumentBuilder();
return builder.parse(new ByteArrayInputStream(xmlString.getBytes()));
StringparseDocumentIntoStringXML(Document document)
Converts a Document into String representation UTF-8 conversion
StringWriter output = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(document), new StreamResult(output));
String newObjectResourceXML = output.toString();
return newObjectResourceXML;