Example usage for javax.xml.parsers DocumentBuilderFactory newDocumentBuilder

List of usage examples for javax.xml.parsers DocumentBuilderFactory newDocumentBuilder

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilderFactory newDocumentBuilder.

Prototype


public abstract DocumentBuilder newDocumentBuilder() throws ParserConfigurationException;

Source Link

Document

Creates a new instance of a javax.xml.parsers.DocumentBuilder using the currently configured parameters.

Usage

From source file:Main.java

/**
 * Loads an XML stream into a Document instance
 * //from   ww w  .  j a v a 2  s. c o  m
 * @param is
 * @return XML file loaded in a document
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document loadDoc(InputStream is) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(is);
}

From source file:Main.java

public static Document fileAsDocument(final File file)
        throws ParserConfigurationException, SAXException, IOException {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final InputSource is = new InputSource(new FileReader(file));
    return builder.parse(is);

}

From source file:Main.java

/**
 * Gets Document representation of xml file
 * //w ww .ja  v  a 2 s . c o m
 * @param file
 *            xml file
 * @return Document object
 */
public static Document getXmlDocument(File file) {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(file);
        return doc;
    } catch (Exception e) {
        throw new RuntimeException("Could not parse XML document", e);
    }
}

From source file:Main.java

/**
 * This method will Read the XML and act accordingly
 *
 * @param xmlString - the XML String/*from   w  ww .  j a  v a 2s  .  c o  m*/
 * @return the list of elements within the XML
 */
public static Document readXML(String xmlString)
        throws SAXParseException, SAXException, ParserConfigurationException, IOException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    InputSource xmlStream = new InputSource();
    xmlStream.setCharacterStream(new StringReader(xmlString));
    return dBuilder.parse(xmlStream);
}

From source file:Main.java

/**
 * Parse XML//from  w w  w .  ja v  a 2 s.  com
 * @throws ParserConfigurationException 
 * @throws IOException 
 * @throws SAXException 
 * */
public static Document parseXMLFromInputStream(InputStream is)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(new InputSource(new InputStreamReader(is)));
    return doc;
}

From source file:Main.java

public static Document createDocument() throws ParserConfigurationException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    return docBuilder.newDocument();
}

From source file:Main.java

public static DocumentBuilder getDocumentBuilder()
        throws FactoryConfigurationError, ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    return factory.newDocumentBuilder();
}

From source file:Main.java

public static Document readDocument(String filePath)
        throws ParserConfigurationException, SAXException, IOException {
    File file = new File(filePath);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = dbFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(file);
    document.getDocumentElement().normalize();
    return document;
}

From source file:Main.java

public static String getBrandName() {
    try {/*from   w ww.  j  av  a 2s  . com*/
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc = builder.parse(new File("configTV.xml"));

        NodeList nl = doc.getElementsByTagName("Name");
        Node classNode = nl.item(0).getFirstChild();
        String Name = classNode.getNodeValue().trim();
        return Name;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

private static DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {

    DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = fact.newDocumentBuilder();

    builder.setErrorHandler(null);/*from w w  w  . j  av a2 s .  c  om*/

    return builder;
}