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

/**
 * Reads an xml string into XML Document.
 * //from  www .j a  va  2s. c om
 * @param xmlStr String containing xml
 * @return xml Document
 * @throws Exception
 */
public static Document readXmlString(String xmlStr) throws Exception {

    Document doc = null;

    try {

        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(false);

        DocumentBuilder builder = domFactory.newDocumentBuilder();
        InputSource inStream = new InputSource();
        inStream.setCharacterStream(new StringReader(xmlStr));
        doc = builder.parse(inStream);
    } catch (ParserConfigurationException e) {
        throw new Exception(e.getMessage(), e);
    } catch (SAXException e) {
        throw new Exception(e.getMessage(), e);
    } catch (IOException e) {
        throw new Exception(e.getMessage(), e);
    } catch (Exception e) {
        throw new Exception(e.getMessage(), e);
    }

    return doc;

}

From source file:Main.java

public static DocumentBuilderFactory createNonValidatingDocumentBuilderFactory()
        throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setValidating(false);//from w  ww  .  ja  v  a2 s  . c  om
    factory.setNamespaceAware(true);
    factory.setFeature("http://xml.org/sax/features/namespaces", false);
    factory.setFeature("http://xml.org/sax/features/validation", false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    return factory;
}

From source file:Main.java

public static Document CreateDocument(Path xml, Path xsd)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*w  ww  .  j av a2 s.  c  om*/
    factory.setNamespaceAware(true);
    factory.setIgnoringComments(true);
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    if (xsd != null) {
        factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xsd.toString());
    }
    return factory.newDocumentBuilder().parse(xml.toString());
}

From source file:Main.java

/**
 * @return//w  w  w. j av  a 2  s  . co m
 * @throws ParserConfigurationException
 */
public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
    if (parser == null) {
        // JPT: Remove xerces use
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        documentBuilderFactory.setValidating(false);
        parser = documentBuilderFactory.newDocumentBuilder();
    }

    return parser;
}

From source file:Main.java

/**
 * Metodo para carregar o arquivo XML assinado e garantir que o XML esta bem formatado.
 * //from w  ww  .j  a v  a  2s .  c o m
 * @param xmlFile arquivo XML assinado.
 * 
 * @return org.w3c.dom.Document 
 * @throws ParserConfigurationException 
 * @throws IOException 
 * @throws SAXException 
 */

public static Document carregarArquivoXML(InputStream xmlFile)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document doc = null;
    DocumentBuilder db;
    db = dbf.newDocumentBuilder();
    doc = db.parse(xmlFile);
    return doc;
}

From source file:Main.java

/**
 * Metodo para carregar uma string contendo um arquivo xml assinado.
 * /*  w  w  w.  j a  v a  2 s  .c o  m*/
 * @param xmlString -> XML assinado.
 * 
 * @return org.w3c.dom.Document 
 * @throws ParserConfigurationException 
 * @throws IOException 
 * @throws SAXException 
 */

public static Document carregarArquivoXML(String xmlString)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document doc = null;
    DocumentBuilder db;
    db = dbf.newDocumentBuilder();
    doc = db.parse(new InputSource(new StringReader(xmlString)));
    return doc;
}

From source file:Main.java

/**
 * Metodo para carregar o arquivo XML assinado e garantir que o XML esta bem formatado.
 * //from  w  ww  . j  a v a2 s .c o m
 * @param xmlSource arquivo XML assinado.
 * 
 * @return org.w3c.dom.Document 
 * @throws ParserConfigurationException 
 * @throws IOException 
 * @throws SAXException 
 */

public static Document carregarArquivoXML(InputSource xmlSource)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document doc = null;
    DocumentBuilder db;
    db = dbf.newDocumentBuilder();
    doc = db.parse(xmlSource);
    return doc;
}

From source file:Main.java

public static Node openFile(String fileName) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);// www .  j a  va 2  s  .co  m
    factory.setNamespaceAware(false);
    DocumentBuilder builder;
    Document document = null;
    try {
        builder = factory.newDocumentBuilder();
        document = builder.parse(new File(fileName));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return document;
}

From source file:Main.java

private static Document toDocument(InputSource in) throws SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setIgnoringComments(true);/*from  ww w  .  j a v  a2  s  . c o  m*/

    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        //will probably never be thrown because we're not doing anything fancy with the configuration
        throw new RuntimeException(e);
    }

    return builder.parse(in);
}

From source file:Main.java

/**
 * Read input stream into XML Document element
 * //w ww . j ava2s  . c  om
 * @param is
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document readXML(InputStream is) throws ParserConfigurationException, SAXException, IOException {
    javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);

    javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();

    org.w3c.dom.Document doc = db.parse(is);

    return doc;
}