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

/**
 * Read a XML text file to XML Document object
 *  /* w w w.j  a  va  2s .  co m*/
 * @param xmlFile
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document readXML(String xmlFile) 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(xmlFile);

    return doc;
}

From source file:Main.java

public static Document loadFromStream(InputStream inputStream) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);//from  w w  w  .ja  v  a  2s.  c  om
    factory.setNamespaceAware(false);

    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setEntityResolver(new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            //System.out.println("Ignoring " + publicId + ", " + systemId);
            return new InputSource(new StringReader(""));
        }
    });

    //System.out.println(StringUtil.toString(inputStream));

    Document doc = builder.parse(inputStream);
    return doc;
}

From source file:Main.java

/**
 * getDocument w/InputStream/*from   w  w w.ja v a  2  s .c om*/
 * @throws ParserConfigurationException
 * @throws IOException
 * @throws SAXException
 */
public static Document getDocument(final InputStream inStream)
        throws ParserConfigurationException, SAXException, IOException {
    Document document;

    DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();

    dfactory.setNamespaceAware(true);

    DocumentBuilder dbuilder = dfactory.newDocumentBuilder();

    document = dbuilder.parse(inStream);

    return document;
}

From source file:Main.java

public static Document createDocument() throws ParserConfigurationException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    docFactory.setNamespaceAware(true);
    return docBuilder.newDocument();

}

From source file:Main.java

public static String setAttribute(String xmlStr, String tagName, String attrName, String newValue) {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(false); // never forget this!
    int i, j;//from   w  w w.java2s  .  co m
    Document doc = null;
    DocumentBuilder builder = null;

    StringWriter stringOut = new StringWriter();
    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 {
            logger.debug("value of " + tagName + " attribute: " + attrName + " = "
                    + nodeList.item(0).getAttributes().getNamedItem(attrName).getNodeValue());
            nodeList.item(0).getAttributes().getNamedItem(attrName).setNodeValue(newValue);

            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(stringOut);
            // StreamResult result = new StreamResult(new File(filepath));
            transformer.transform(source, result);
            logger.debug("Updated XML: \n" + stringOut.toString());
        }
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }

    return stringOut.toString();
}

From source file:Main.java

public static Document parse(String xmlStr) throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setValidating(false);/*from   w  ww.  j a  v a  2  s .  c o  m*/

    factory.setNamespaceAware(false);

    factory.setIgnoringComments(true);

    factory.setIgnoringElementContentWhitespace(false);

    factory.setCoalescing(false);

    factory.setExpandEntityReferences(true);

    DocumentBuilder builder = factory.newDocumentBuilder();

    ByteArrayInputStream inputStream = new ByteArrayInputStream(xmlStr.getBytes());

    return builder.parse(inputStream);

}

From source file:Main.java

public static Document loadXMLFrom(final InputSource is)
        throws ParserConfigurationException, IOException, SAXException {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = null;
    builder = factory.newDocumentBuilder();
    assert builder != null;
    final Document doc = builder.parse(is);
    return doc;//from   w  w w  . ja  va  2s . co m
}

From source file:Main.java

/**
 * Loads an XML document from the given file.
 * @param file XML file to load./*from  w  w  w.  j  av  a 2s.  co  m*/
 * @param isValidating true: XML doc is validated against the DTD specifed
 *   within the XML document, false: otherwise.
 *   This does not work for XML Schemata! See {@link #load(File, Schema)}
 *   for that.
 * @param isNamespaceAware true: XML doc is namespace aware, false: otherwise.
 * @return Returns an XML document.
 * @throws IOException Throws an exception if the file couldn't be read.
 */
static public Document load(File file, boolean isValidating, boolean isNamespaceAware) throws IOException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(isValidating);
        factory.setNamespaceAware(isNamespaceAware);
        DocumentBuilder builder = factory.newDocumentBuilder();
        return (builder.parse(file));
    } catch (ParserConfigurationException pce) {
        throw new IOException("Parser configuration error: " + pce);
    } catch (SAXException se) {
        throw new IOException("XML parsing error: " + se);
    }
}

From source file:Main.java

public static org.w3c.dom.Document configureJDomDocFromResource(String fileName) {
    org.w3c.dom.Document doc = null;
    InputStream in = ClassLoader.getSystemResourceAsStream(fileName);

    if (in != null) {
        try {//from ww  w.jav a  2 s  . c o  m
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(false);
            factory.setNamespaceAware(false);
            doc = factory.newDocumentBuilder().parse(in);
        } catch (Exception e) {
            //                Debug.error(e);
        }
    } else {
        //            Debug.error("could not find file " + fileName);
    }

    return doc;
}

From source file:Main.java

public static Document getXmlDocFromURI(InputStream is) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    dbf.setNamespaceAware(true);
    return dbf.newDocumentBuilder().parse(is);
}