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

/**
 * Loads an XML document from the input stream into a DOM Document.
 *
 * @param is the input stream to load from
 *
 * @return the new Document/*from   ww  w. j a  v  a2  s  .com*/
 *
 * @throws SAXException                 if a SAX parsing error occurs
 * @throws IOException                  if an IO error occurs
 * @throws ParserConfigurationException if a JAXP configuration parsing
 *                                      error occurs
 */
public static Document loadXMLFrom(final InputStream is)
        throws SAXException, IOException, ParserConfigurationException {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = null;
    builder = factory.newDocumentBuilder();
    assert builder != null;
    final Document doc = builder.parse(is);
    is.close();
    return doc;
}

From source file:Main.java

public static String getProcessIdFromBpmn(final String bpmn) {
    String processId = null;/*from  w w  w .j a v  a  2 s .c om*/
    try {
        final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        final Document doc = domFactory.newDocumentBuilder()
                .parse(new ByteArrayInputStream(bpmn.getBytes("UTF-8")));
        final XPath xpath = XPathFactory.newInstance().newXPath();
        xpath.setNamespaceContext(new NamespaceContext() {
            @Override
            public Iterator<?> getPrefixes(final String namespaceURI) {
                // Not used in this context.
                return null;
            }

            @Override
            public String getPrefix(final String namespaceURI) {
                // Not used in this context.
                return null;
            }

            @Override
            public String getNamespaceURI(final String prefix) {
                // Only require the URI for the bpmn2 NS.
                return BPMN2_NAMESPACE_URI;
            }
        });
        final XPathExpression expr = xpath.compile(BPMN_PROCESS_ID_XPATH_EXPR);

        final Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
        processId = node.getAttributes().getNamedItem(BPMN_PROCESS_ID_ATTR).getNodeValue();
    } catch (final Exception e) {
        e.printStackTrace();
    }
    return processId;
}

From source file:Main.java

/**
 * Read application-context file, and return fully qualified class name for
 * given <code>beanName</code>
 * //from   w w w  . ja va2  s  .  co  m
 * @param beanName
 * @return
 * 
 */
public static String getFullyQualifiedClass(String beanName) {

    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

    docBuilderFactory.setNamespaceAware(true);

    String nodeValue = "";

    try {
        DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
        Document doc = builder.parse("application-context.xml");

        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile("//bean[@name='" + beanName + "']/@class");

        Object result = expr.evaluate(doc, XPathConstants.NODESET);

        NodeList nodes = (NodeList) result;
        if (nodes.getLength() > 0) {
            nodeValue = nodes.item(0).getNodeValue();
        }
    } catch (ParserConfigurationException parserConfigurationException) {
        parserConfigurationException.printStackTrace();
    } catch (IOException ioException) {
        ioException.printStackTrace();
    } catch (SAXException saxException) {
        saxException.printStackTrace();
    } catch (XPathExpressionException xPathExpressionException) {
        xPathExpressionException.printStackTrace();
    }

    return nodeValue;
}

From source file:Main.java

/**
 * Applies a stylesheet (that receives parameters) to a given xml document.
 * //from   w  ww.  j  av  a 2 s  .co m
 * @param xmlDocument
 *            the xml document to be transformed
 * @param parameters
 *            the hashtable with the parameters to be passed to the
 *            stylesheet
 * @param xsltFilename
 *            the filename of the stylesheet
 * @return the transformed xml document
 * @throws Exception
 */
public static Document transformDocument(Document xmlDocument, Map<String, String> parameters,
        String xsltFilename) throws Exception {

    // Generate a Transformer.
    Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFilename));

    // set transformation parameters
    if (parameters != null) {
        for (Map.Entry<String, String> param : parameters.entrySet()) {
            transformer.setParameter(param.getKey(), param.getValue());
        }

    }

    // Create an empty DOMResult object for the output.
    DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
    dFactory.setNamespaceAware(true);
    DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
    Document dstDocument = dBuilder.newDocument();

    DOMResult domResult = new DOMResult(dstDocument);

    // Perform the transformation.
    transformer.transform(new DOMSource(xmlDocument), domResult);
    // Now you can get the output Node from the DOMResult.
    return dstDocument;
}

From source file:Main.java

public static Document createDocument() {
    // creo la factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    // setX (che tipo di document builder ci deve produrre)

    dbf.setNamespaceAware(false);
    // se metti a true la valiazione il parser fa la validazione (prende la dtd associata e valida l'xml)
    dbf.setValidating(false);/*ww w . j  ava 2  s.  co  m*/
    try {
        // dalla factory creo il DocumentBuilder
        DocumentBuilder db = dbf.newDocumentBuilder();
        return db.newDocument();
    } catch (ParserConfigurationException ex) {
        System.err.println("Impossibile creare il parser XML!");
    }

    return null;
}

From source file:Main.java

/**
 * Gets the node list from the given xml file and the given xpath expression.
 * // w  w  w . ja  v a 2s  . c  o  m
 * @param xml
 *            the xml
 * @param xpathExpression
 *            the xpath expression
 * @return the node list
 * @throws XPathExpressionException
 *             the x path expression exception
 * @throws ParserConfigurationException
 *             the parser configuration exception
 * @throws SAXException
 *             the sAX exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static NodeList getNodeList(File xml, String xpathExpression)
        throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(xml);
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile(xpathExpression);

    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    return nodes;
}

From source file:Main.java

/**
 * Gets the node list from the given xml file and the given xpath expression.
 * /*  w ww  .java 2s . co m*/
 * @param xml
 *            the xml file as string.
 * @param xpathExpression
 *            the xpath expression as string.
 * @return the node list
 * @throws XPathExpressionException
 *             the x path expression exception
 * @throws ParserConfigurationException
 *             the parser configuration exception
 * @throws SAXException
 *             the sAX exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static NodeList getNodeList(String xml, String xpathExpression)
        throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(xml);
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile(xpathExpression);

    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    return nodes;
}

From source file:com.cloudseal.spring.client.namespace.Utility.java

private static Element domFromReader(final Reader reader)
        throws ParserConfigurationException, IOException, SAXException {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    return factory.newDocumentBuilder().parse(new InputSource(reader)).getDocumentElement();
}

From source file:Main.java

public static Document parseDocument(String xml, boolean namespaceaware)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(namespaceaware);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new InputSource(new StringReader(xml)));
}

From source file:Main.java

/**
 * Parses the document./*  w  w w . jav  a 2s. c  o m*/
 * 
 * @param is
 *        the is
 * @param namespaceaware
 *        the namespaceaware
 * @return the document
 * @throws ParserConfigurationException
 *         the parser configuration exception
 * @throws SAXException
 *         the sAX exception
 * @throws IOException
 *         Signals that an I/O exception has occurred.
 */
public static Document parseDocument(InputStream is, boolean namespaceaware)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(namespaceaware);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(is);
}