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:com.mingo.parser.xml.dom.DocumentBuilderFactoryCreator.java

/**
 * Creates DocumentBuilderFactory./*ww w . j  a  v a 2 s.  co m*/
 *
 * @param parserConfiguration {@link ParserConfiguration}
 * @return DocumentBuilderFactory a factory API that enables applications to obtain a
 *         parser that produces DOM object trees from XML documents
 * @throws ParserConfigurationException {@link ParserConfigurationException}
 */
public static DocumentBuilderFactory createDocumentBuilderFactory(ParserConfiguration parserConfiguration)
        throws ParserConfigurationException {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setValidating(parserConfiguration.isValidate());
    documentBuilderFactory.setNamespaceAware(parserConfiguration.isNamespaceAware());
    documentBuilderFactory.setFeature(DYNAMIC_VALIDATION, true);
    List<Source> sourceList = createSchemaSources(parserConfiguration.getXsdSchemaPaths());
    if (CollectionUtils.isNotEmpty(sourceList)) {
        documentBuilderFactory.setSchema(createSchema(sourceList));
    }
    return documentBuilderFactory;
}

From source file:eu.optimis.mi.gui.server.XmlUtil.java

@SuppressWarnings("unused") //FIXME Remove?
private static Document getDocument(String xml) {
    try {/*from www .ja  v  a  2  s.  c  om*/
        // Create a builder factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);

        return factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
    } catch (SAXException e) {
        return null;
    } catch (ParserConfigurationException e) {
        return null;
    } catch (IOException e) {
        return null;
    }
}

From source file:gov.nih.nci.cabig.caaers.utils.XmlValidator.java

public static boolean validateAgainstSchema(String xmlContent, String xsdUrl, StringBuffer validationResult) {
    boolean validXml = false;
    try {/*from w  w  w. j av a 2 s .c o m*/
        // parse an XML document into a DOM tree
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setValidating(false);
        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder();
        Document document = parser.parse(new InputSource(new StringReader(xmlContent)));

        // create a SchemaFactory capable of understanding WXS schemas
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // load a WXS schema, represented by a Schema instance
        Source schemaFile = new StreamSource(getResources(xsdUrl)[0].getFile());

        Schema schema = schemaFactory.newSchema(schemaFile);
        // create a Validator instance, which can be used to validate an instance document
        Validator validator = schema.newValidator();
        // validate the DOM tree
        validator.validate(new DOMSource(document));
        validXml = true;
    } catch (FileNotFoundException ex) {
        throw new CaaersSystemException("File Not found Exception", ex);
    } catch (IOException ioe) {
        validationResult.append(ioe.getMessage());
        logger.error(ioe.getMessage());
    } catch (SAXParseException spe) {
        validationResult.append("Line : " + spe.getLineNumber() + " - " + spe.getMessage());
        logger.error("Line : " + spe.getLineNumber() + " - " + spe.getMessage());
    } catch (SAXException e) {
        validationResult.append(e.toString());
        logger.error(e.toString());
    } catch (ParserConfigurationException pce) {
        validationResult.append(pce.getMessage());
    }
    return validXml;
}

From source file:Utils.java

/**
 * Read XML as DOM./*from  w  ww.j a  va  2s .c o m*/
 */
public static Document readXml(InputStream is) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);
    // dbf.setCoalescing(true);
    // dbf.setExpandEntityReferences(true);

    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());

    // db.setErrorHandler( new MyErrorHandler());

    return db.parse(is);
}

From source file:org.opencastproject.remotetest.util.WorkflowUtils.java

/**
 * Parses the workflow instance represented by <code>xml</code> and extracts the workflow identifier.
 * //  w  w w.  ja v a2  s .  com
 * @param xml
 *          the workflow instance
 * @return the workflow instance
 * @throws Exception
 *           if parsing fails
 */
public static String getWorkflowInstanceId(String xml) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(IOUtils.toInputStream(xml, "UTF-8"));
    return ((Element) XPathFactory.newInstance().newXPath().compile("/*").evaluate(doc, XPathConstants.NODE))
            .getAttribute("id");
}

From source file:org.opencastproject.remotetest.util.WorkflowUtils.java

/**
 * Parses the workflow instance represented by <code>xml</code> and extracts the workflow state.
 * /*from ww  w  .  j av  a2s  .  co m*/
 * @param xml
 *          the workflow instance
 * @return the workflow state
 * @throws Exception
 *           if parsing fails
 */
public static String getWorkflowState(String xml) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(IOUtils.toInputStream(xml, "UTF-8"));
    return ((Element) XPathFactory.newInstance().newXPath().compile("/*").evaluate(doc, XPathConstants.NODE))
            .getAttribute("state");
}

From source file:fr.aliasource.webmail.server.proxy.client.http.DOMUtils.java

public static Document parse(InputStream is)
        throws SAXException, IOException, ParserConfigurationException, FactoryConfigurationError {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder builder = dbf.newDocumentBuilder();
    return builder.parse(is);
}

From source file:fr.aliasource.webmail.server.proxy.client.http.DOMUtils.java

public static Document createDoc(String namespace, String rootElement)
        throws ParserConfigurationException, FactoryConfigurationError {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder builder = dbf.newDocumentBuilder();
    DOMImplementation di = builder.getDOMImplementation();
    return di.createDocument(namespace, rootElement, null);
}

From source file:hoot.services.utils.XmlDocumentBuilder.java

/**
 * Parses an XML string into a DOM//w w  w . j  av a2s.  com
 * 
 * @param xml an XML string
 * @param namespaceAware determines whether namespaces are respected during the parsing
 * @return an XML DOM
 * @throws SAXException
 * @throws IOException
 * @throws ParserConfigurationException
 */
public static Document parse(String xml, boolean namespaceAware)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(namespaceAware); // never forget this!
    DocumentBuilder builder;
    builder = domFactory.newDocumentBuilder();

    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xml));

    return builder.parse(is);
}

From source file:Utils.java

public static Document readXml(StreamSource is) throws SAXException, IOException, ParserConfigurationException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);// w  ww  . j  av a 2s  .c  om
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);
    // dbf.setCoalescing(true);
    // dbf.setExpandEntityReferences(true);

    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());

    // db.setErrorHandler( new MyErrorHandler());
    InputSource is2 = new InputSource();
    is2.setSystemId(is.getSystemId());
    is2.setByteStream(is.getInputStream());
    is2.setCharacterStream(is.getReader());

    return db.parse(is2);
}