Java XML DOM Parse parseToDOM(InputStream stream)

Here you can find the source of parseToDOM(InputStream stream)

Description

Parses an XML stream into a DOM document.

License

Open Source License

Parameter

Parameter Description
stream The XML stream to parse

Exception

Parameter Description
Exception Software configuration problem or problem with underlying stream

Return

The DOM document resulting from parsing

Declaration

public static Document parseToDOM(InputStream stream) throws Exception 

Method Source Code


//package com.java2s;
/*// w ww.  j a  v  a2  s.  com
 * Copyright 2016 Mentor Graphics Corporation. All Rights Reserved.
 * <p>
 * Recipients who obtain this code directly from Mentor Graphics use it solely
 * for internal purposes to serve as example Java web services.
 * This code may not be used in a commercial distribution. Recipients may
 * duplicate the code provided that all notices are fully reproduced with
 * and remain in the code. No part of this code may be modified, reproduced,
 * translated, used, distributed, disclosed or provided to third parties
 * without the prior written consent of Mentor Graphics, except as expressly
 * authorized above.
 * <p>
 * THE CODE IS MADE AVAILABLE "AS IS" WITHOUT WARRANTY OR SUPPORT OF ANY KIND.
 * MENTOR GRAPHICS OFFERS NO EXPRESS OR IMPLIED WARRANTIES AND SPECIFICALLY
 * DISCLAIMS ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
 * OR WARRANTY OF NON-INFRINGEMENT. IN NO EVENT SHALL MENTOR GRAPHICS OR ITS
 * LICENSORS BE LIABLE FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING LOST PROFITS OR SAVINGS) WHETHER BASED ON CONTRACT, TORT
 * OR ANY OTHER LEGAL THEORY, EVEN IF MENTOR GRAPHICS OR ITS LICENSORS HAVE BEEN
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * <p>
 */

import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import java.io.InputStream;

public class Main {
    private static DocumentBuilder _domBuilder;

    /**
     * Parses an XML stream into a DOM document.
     * @param stream The XML stream to parse
     * @return The DOM document resulting from parsing
     * @throws Exception Software configuration problem or problem with underlying stream
     */
    public static Document parseToDOM(InputStream stream) throws Exception {
        return getDOMDocBuilder().parse(stream);
    }

    private static DocumentBuilder getDOMDocBuilder() throws ParserConfigurationException {
        if (_domBuilder == null) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            _domBuilder = factory.newDocumentBuilder();
        }
        return _domBuilder;
    }
}

Related

  1. parseDom(InputStream byteArrayInputStream)
  2. parseDom(Reader reader)
  3. parseDOMConfigFile(String folderPath, String configFileName)
  4. parseDomFromFile(File doc)
  5. parseFileToXML(Transformer transformer, DOMSource source, File file)
  6. parseToDom(String content)
  7. parseXMLToDOM(String xml_str)