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

public static Document fileToDocument(String filename) throws Exception {
    System.out.println("[xml] read document from '" + filename + "'.");

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder db = factory.newDocumentBuilder();
    try (FileInputStream sr = new FileInputStream(filename)) {
        Document document = db.parse(sr);
        return document;
    }/*  w w  w.  j  av  a2 s  .  c  om*/

}

From source file:Main.java

public static Document load(String src) throws IOException, SAXException, ParserConfigurationException {
    Document doc = null;//from w w w .j  a  v  a  2 s .c  o m
    InputSource is = new InputSource(new StringReader(src));
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    doc = docBuilder.parse(is);

    return doc;
}

From source file:Main.java

private static DocumentBuilder setUpDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    return documentBuilderFactory.newDocumentBuilder();
}

From source file:Main.java

private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
    if (builder == null) {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        builder = domFactory.newDocumentBuilder();
    }//from ww w.  j a  v a 2s  . com
    return builder;
}

From source file:Main.java

/**
 * Start a new XML Document.//from w w  w . ja v a 2s . c  om
 * @param rootName The name of the Document root Element (created here)
 * @return the Document
 * @throws DomException
 */
public static Document createXmlDocument(String rootName) throws Exception {
    DocumentBuilderFactory factory;

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

    Document document = factory.newDocumentBuilder().newDocument();
    Element root = document.createElement(rootName);

    document.appendChild(root);
    return document;
}

From source file:Main.java

/**
 * Returns root tagname//from w  w w . j av  a2s  . co m
 *
 * @param xml  The xml document to be searched.
 * @return String result.
 */
public static String getRootTagName(String xml) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder builder = dbf.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    return document.getDocumentElement().getTagName();
}

From source file:Main.java

public static DocumentBuilder getDocumentBuilder() {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setIgnoringComments(true);/*w  w  w.j  ava  2  s . c o  m*/
    DocumentBuilder db;
    try {
        db = factory.newDocumentBuilder();
        return db;
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Document newDocument() {
    try {// w  w  w  .  ja  v  a 2  s .  c o  m
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        dFactory.setNamespaceAware(true);
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        return builder.newDocument();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Document stringToXML(String xmlString) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    Document document = null;//from www. ja  v a 2  s  .c  om
    try {
        document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xmlString)));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return document;
}

From source file:Main.java

public static Document stringToDocument(final String string)
        throws ParserConfigurationException, UnsupportedEncodingException, SAXException, IOException {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    final DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new InputSource(new ByteArrayInputStream(string.getBytes(ENCODING))));
}