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

/**
 * Method to create a document based on xml string.
 * /*from   w w  w.jav  a  2 s .  c om*/
 * @param content (xml)
 * @return Document file for given xml content
 * @throws SAXException
 * @throws IOException
 * @throws ParserConfigurationException
 */
public static Document createDocumentString(String content)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
    fac.setNamespaceAware(true);
    Document doc = fac.newDocumentBuilder().parse(new InputSource(new StringReader(content)));
    return doc;
}

From source file:Main.java

/**
 *
 * @param xmlContent//from  ww  w  . ja va  2  s  .  c o m
 * @param charset
 * @return
 * @throws SAXException
 * @throws IOException
 * @throws ParserConfigurationException
 */
private static Document parse(String xmlContent, Charset charset)
        throws SAXException, IOException, ParserConfigurationException {

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(false);
    documentBuilderFactory.setValidating(false);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(new ByteArrayInputStream(xmlContent.getBytes(charset)));

    return document;
}

From source file:Main.java

/**
 * @param is/*from  ww w  .  java  2s .  c  o m*/
 * @return
 * @throws ParserConfigurationException
 * @throws IOException
 * @throws SAXException
 */
public static Document parseInputStream(InputStream is) throws IOException {
    try {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(false);
        domFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        return builder.parse(is);
    } catch (Exception e) {
        throw new IOException("Error parsing XML Stream", e);
    }
}

From source file:Main.java

public static Document convertToDocument(Node node) throws ParserConfigurationException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document newDocument = builder.newDocument();
    newDocument.setXmlStandalone(true);/*from ww  w .  j a v a2s.  co m*/
    Node importedNode = newDocument.importNode(node, true);
    newDocument.appendChild(importedNode);
    return newDocument;
}

From source file:Main.java

static public DocumentBuilder getBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    return builder;
}

From source file:com.cxf.sts.WSO2STSTest.java

private static Element loadPolicy(String xmlPath) {
    try {//from ww w .j a va2 s.  c o m
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document d = db.parse(new File(xmlPath));
        return d.getDocumentElement();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

private static DocumentBuilder newDocumentBuilder(Schema schema, boolean isNamespaceAware,
        boolean isXIncludeAware) throws SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setNamespaceAware(isNamespaceAware);
    factory.setXIncludeAware(isXIncludeAware);
    if (schema != null) {
        factory.setSchema(schema);//from  w  w  w .j a  v  a2 s  . c  o m
        factory.setValidating(false);
    }
    DocumentBuilder builder = null;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        // there is something seriously wrong
        throw new RuntimeException(ex);
    }
    return builder;
}

From source file:Main.java

public static String getAttribute(String xmlStr, String tagName, String attrName) {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(false); // never forget this!
    Document doc = null;/*from   ww  w. j a  v  a 2 s  . c o  m*/
    DocumentBuilder builder = null;
    String value = null;

    try {
        builder = domFactory.newDocumentBuilder();
        doc = builder.parse(new ByteArrayInputStream(xmlStr.getBytes()));
        // Get the root element
        Node rootNode = doc.getFirstChild();
        NodeList nodeList = doc.getElementsByTagName(tagName);
        if ((nodeList.getLength() == 0) || (nodeList.item(0).getAttributes().getNamedItem(attrName) == null)) {
            logger.error("Either node " + tagName + " or attribute " + attrName + " not found.");
        } else {
            value = nodeList.item(0).getAttributes().getNamedItem(attrName).getNodeValue();
            logger.debug("value of " + tagName + " attribute: " + attrName + " = " + value);
        }
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }

    return value;
}

From source file:Main.java

public static Element byteArrayToElement(byte[] cont) {
    if (cont == null || cont.length <= 0)
        return null;

    try {// w  w w . jav a 2 s .  com
        ByteArrayInputStream bais = new ByteArrayInputStream(cont);
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setNamespaceAware(false);
        docBuilderFactory.setValidating(false);
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

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

From source file:Main.java

private static DocumentBuilder getBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    DocumentBuilder builder = factory.newDocumentBuilder();
    // prevent DTD entities from being resolved.
    builder.setEntityResolver(new EntityResolver() {
        @Override//from   ww w.j  av  a 2 s  .  c  om
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            return new InputSource(new StringReader(""));
        }
    });

    return builder;
}