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 Node getNode(String text) throws Exception {
    Node ret = null;//from   w  ww  .  j a v  a2s.  c om

    // Transform the text representation to DOM
    DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
    fact.setNamespaceAware(true);

    java.io.InputStream xmlstr = new java.io.ByteArrayInputStream(text.getBytes());

    DocumentBuilder builder = fact.newDocumentBuilder();
    org.w3c.dom.Document doc = builder.parse(xmlstr);

    xmlstr.close();

    ret = doc.getDocumentElement();

    return (ret);
}

From source file:Main.java

public static Document getNormalizedDocument(Reader input)
        throws ParserConfigurationException, SAXException, IOException {

    InputSource is = new InputSource(input);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document d = db.parse(is);/*from w  w  w .  j  av  a 2  s  .  c o m*/
    return d;
    //      return (Document) Nodes.normalizeWhitespace(d); // FIXME: is this required??
}

From source file:Main.java

public static Document getXMLDocumentFromString(String xml)
        throws SAXException, IOException, ParserConfigurationException {
    Document doc = null;/*  w  w w  . ja  v  a 2s . com*/
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware(true);

    DocumentBuilder db = docBuilderFactory.newDocumentBuilder();
    doc = db.parse(new InputSource(new StringReader(xml)));
    return doc;
}

From source file:Main.java

/**
 * Creates an empty JAXP XML document./*from   ww  w. j a  v a 2 s .c  o  m*/
 * 
 * @return An empty JAXP XML document.
 */
public static Document getNewEmptyJAXPDoc() {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder;

    try {
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
        return documentBuilder.newDocument();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static Document getXMLDocumentFromScratch(String rootElementName)
        throws SAXException, IOException, ParserConfigurationException {
    Document doc = null;/*ww w  . ja  va 2  s.  co m*/
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware(true);

    DocumentBuilder db = docBuilderFactory.newDocumentBuilder();
    doc = db.parse(new InputSource(new StringReader("<" + rootElementName + "></" + rootElementName + ">")));
    return doc;
}

From source file:Main.java

/**
 * Returns an empty document to which nodes can be appended.
 * @return an empty document to which nodes can be appended.
 *///  w w  w. ja v a2 s.  co m
public static Document getEmptyDoc() {

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

    try {
        return factory.newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException(e); // shouldn't be reached
    }

}

From source file:Main.java

/**
 * Parse the supplied string into an xml document.
 * //w ww.ja v a2  s .co  m
 * @param content
 * @return
 */
public static Document parseString(String content) {
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

        //parse the stream into a document
        return documentBuilder.parse(new ByteArrayInputStream(content.getBytes()));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * This function does not support validation via DTD.
 *//*from  w  ww .ja  v  a  2s. c  o  m*/
public static Document getXMLFromString(String str)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new ByteArrayInputStream(str.getBytes()));
}

From source file:Main.java

public static Element stringToElement(String str) {
    StringReader sr = new StringReader(str);

    try {/*from  ww w .j a v  a  2  s .  c  om*/
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setNamespaceAware(false);
        docBuilderFactory.setValidating(false);
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        InputSource is = new InputSource(sr);
        // is.setEncoding("gb2312");
        Document doc = docBuilder.parse(is);
        return doc.getDocumentElement();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Document loadXMLFrom(InputStream inputStream) throws Exception {
    Document doc = null;//from   w w  w  .  j a v a2  s.  c o m
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    javax.xml.parsers.DocumentBuilder builder = null;
    try {
        builder = factory.newDocumentBuilder();
        doc = builder.parse(inputStream);
    } catch (ParserConfigurationException pce) {
        throw new Exception(pce.getMessage());
    } catch (SAXException se) {
        throw new Exception(se.getMessage());
    } catch (IOException ioe) {
        throw new Exception(ioe.getMessage());
    } finally {
        inputStream.close();
    }
    return doc;
}