Example usage for javax.xml.parsers DocumentBuilderFactory setNamespaceAware

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

Introduction

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

Prototype


public void setNamespaceAware(boolean awareness) 

Source Link

Document

Specifies that the parser produced by this code will provide support for XML namespaces.

Usage

From source file:Main.java

/**
 * Loads an xml file on disk into a document
 *
 * @param  xmlFileName - name of the file on disk, including path
 * @return Document    - loaded from the specified xml file on disk
 *///from   ww  w.j  a va  2  s . c om
public static Document loadXmlFromFile(String xmlFileName) {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);

    try {
        return docFactory.newDocumentBuilder().parse(new File(xmlFileName));
    } catch (ParserConfigurationException e) {
        throw new RuntimeException("Error setting up UI xml page model parser -- " + e.getMessage());
    } catch (SAXException e) {
        throw new RuntimeException(
                "Error parsing UI xml page model file [" + xmlFileName + "]  -- " + e.getMessage());
    } catch (IOException e) {
        throw new RuntimeException(
                "Error opening or reading UI xml page model file [" + xmlFileName + "]  -- " + e.getMessage());
    }
}

From source file:Main.java

/** Parse the provided {@link File} as XML, decompressing it first if it has a .gz file extension. */
public static Document parseXmlFile(File file) throws IOException {
    InputStream in = toInputStream(file);
    try {/* ww  w . j a v a2  s.  c  om*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(in);
    } catch (Exception e) {
        throw new RuntimeException("failed to parse", e);
    } finally {
        in.close();
    }
}

From source file:Main.java

private static DocumentBuilder createDocumentBuilder() {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {/*from   w w w  .ja  v a  2s . c  om*/
        dbf.setNamespaceAware(false);
        return dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException(e);
    }
}

From source file:Main.java

/**
 * Creates and returns an new document builder factory. This method tries to
 * configure the namespace support for the builder. If the underlying parser
 * does not support namespaces then this method returns a simple
 * DocumentBuilder object./*from  w  w  w.j a v a 2  s  .  c  o  m*/
 * 
 * @return a new document builder
 * @throws ParserConfigurationException
 */
private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true); // never forget this!
    try {
        factory.setFeature("http://xml.org/sax/features/namespaces", true);
    } catch (Throwable t) {
        // Just skip it...
    }
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder;
}

From source file:Main.java

static private Document parseDocument(byte[] bytes)
        throws IOException, ParserConfigurationException, SAXException {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);

    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

    try (InputStream is = new ByteArrayInputStream(bytes)) {
        return documentBuilder.parse(is);
    }/*from  w  ww .j  a  va  2 s  .co  m*/
}

From source file:Main.java

/**
 * Convenience method to parse an input stream into a document
 * @param inputStream//  w w w .j a v a 2  s. c  o m
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document getDocument(InputStream inputStream)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    return db.parse(inputStream);
}

From source file:Main.java

public static Document newDocument() throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = factory.newDocumentBuilder();
    return documentBuilder.newDocument();
}

From source file:Main.java

private static DocumentBuilderFactory getDocumentBuilderFactoryInstance() {
    DocumentBuilderFactory localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
    localDocumentBuilderFactory.setNamespaceAware(true);
    return localDocumentBuilderFactory;
}

From source file:Main.java

private static DocumentBuilder getDocumentBuilder() {
    try {//  w w  w.jav a  2s.c  om
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(false);
        factory.setValidating(false);
        factory.setXIncludeAware(false);
        return factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Read the contents of the specified file into a DOM document
 * @param f the input XML file// w w  w.j  a va  2 s.c om
 * @return the document
 * @throws Exception
 */
public static final Document parseFileToDocument(File f) throws Exception {
    DocumentBuilderFactory docBuilderFact = DocumentBuilderFactory.newInstance();
    docBuilderFact.setNamespaceAware(true);
    DocumentBuilder docBuilder = docBuilderFact.newDocumentBuilder();
    Document document = docBuilder.parse(f);
    return document;
}