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 an InputStream
 *//*from  w  ww  . ja v  a 2 s.co  m*/
public static Document loadXmlDoc(final InputStream stream) {
    Document result = null;
    try {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setExpandEntityReferences(false);
        domFactory.setIgnoringComments(true);//strips comments
        //         domFactory.setIgnoringElementContentWhitespace(true);//would be nice if it worked
        domFactory.setNamespaceAware(true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        result = builder.parse(stream);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:de.tub.av.pe.xcapsrv.XMLValidator.java

public static Document getWellFormedDocument(Reader reader)
        throws NotWellFormedConflictException, InternalServerErrorException {
    try {/*  w w  w.  j a v  a  2 s  . c  o m*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder parser = factory.newDocumentBuilder();
        Document docu = parser.parse(new InputSource(reader));
        //testTheCreatedDocu(docu);
        return docu;
    } catch (SAXException e) {
        throw new NotWellFormedConflictException();
    } catch (IOException e) {
        throw new InternalServerErrorException(e.getMessage());
    } catch (ParserConfigurationException e) {
        throw new InternalServerErrorException(e.getMessage());
    }
}

From source file:de.tub.av.pe.xcapsrv.XMLValidator.java

public static Element getWellFormedDocumentFragment(Reader reader)
        throws NotValidXMLFragmentConflictException, InternalServerErrorException {
    try {//  w w  w  .  ja v a2  s.  com
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(false);
        DocumentBuilder parser = factory.newDocumentBuilder();
        Document dummyDocument = parser.parse(new InputSource(reader));
        return dummyDocument.getDocumentElement();
    } catch (SAXException e) {

        throw new NotValidXMLFragmentConflictException();
    } catch (IOException e) {
        throw new InternalServerErrorException(e.getMessage());
    } catch (ParserConfigurationException e) {
        throw new InternalServerErrorException(e.getMessage());
    }
}

From source file:com.photon.phresco.impl.WindowsApplicationProcessor.java

private static void deleteFeature(File sourceFolderLocation, List<ArtifactGroup> deletedFeatures)
        throws PhrescoException {
    try {//from  w w  w  .  j  ava2  s  .c  o  m
        File path = new File(sourceFolderLocation + File.separator + SOURCE_DIR + File.separator + SRC_DIR
                + File.separator + PROJECT_ROOT + File.separator + PROJECT_ROOT + CSPROJ_FILE);
        if (!path.exists() && CollectionUtils.isNotEmpty(deletedFeatures)) {
            return;
        }
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setNamespaceAware(false);
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(path);
        for (ArtifactGroup deleteFeature : deletedFeatures) {
            String feature = deleteFeature.getName();
            feature = "//Reference[@Include='" + feature + "']";
            XPath xpath = XPathFactory.newInstance().newXPath();
            javax.xml.xpath.XPathExpression expr = xpath.compile(feature);
            Object result = expr.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
            for (int i = 0; i < nodes.getLength(); i++) {
                Node item = nodes.item(i).getParentNode();
                item.getParentNode().removeChild(item);
            }
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(path.toURI().getPath());
        transformer.transform(source, result);

    } catch (XPathExpressionException e) {
        throw new PhrescoException(e);
    } catch (DOMException e) {
        throw new PhrescoException(e);
    } catch (ParserConfigurationException e) {
        throw new PhrescoException(e);
    } catch (SAXException e) {
        throw new PhrescoException(e);
    } catch (IOException e) {
        throw new PhrescoException(e);
    } catch (TransformerConfigurationException e) {
        throw new PhrescoException(e);
    } catch (TransformerException e) {
        throw new PhrescoException(e);
    }
}

From source file:es.usc.citius.composit.transformer.wsc.wscxml.WSCTransformer.java

public static URL obtainXmlBaseUri(InputStream owlStream)
        throws IOException, XPathExpressionException, ParserConfigurationException, SAXException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    // Detect namespaces
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(owlStream);
    // Obtain the document element where the xml:base is
    Element rootElement = document.getDocumentElement();
    String baseUri = rootElement.getAttribute("xml:base");
    return new URL(baseUri);
}

From source file:edu.lternet.pasta.common.XmlUtility.java

/**
 * Parses the provided XML string as a (DOM) document.
 *
 * @param xmlString the XML string to be parsed.
 * @param schema the schema used to validate the provided XML.
 *
 * @return a document derived from the provided XML string.
 *///  w w w. j  a v a  2 s  .  c o m
public static Document xmlStringToDoc(String xmlString, Schema schema) {

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

    InputSource source = new InputSource(new StringReader(xmlString));

    try {

        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setErrorHandler(new XmlParsingErrorHandler(xmlString));
        return builder.parse(source);

    } catch (ParserConfigurationException e) {
        throw new IllegalStateException(e); // shouldn't be reached
    } catch (SAXException e) {
        throw new IllegalStateException(e); // shouldn't be reached
    } catch (IOException e) {
        throw new IllegalStateException(e); // shouldn't be reached
    }

}

From source file:net.bpelunit.util.XMLUtil.java

public static Document parseXML(InputStream in) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    return dBuilder.parse(in);
}

From source file:net.bpelunit.util.XMLUtil.java

/**
 * @param xmlAsString/*  w  w w .  ja  va  2  s  .  c o m*/
 *            document in string form, encoding specified in XML should be
 *            UTF-8
 * @return
 * @throws SAXException
 * @throws IOException
 * @throws UnsupportedEncodingException
 * @throws ParserConfigurationException
 */
public static Document parseXML(String xmlAsString)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    return dBuilder.parse(new ByteArrayInputStream(xmlAsString.getBytes("UTF-8")));
}

From source file:com.marklogic.hub.HubTestBase.java

protected static Document getXmlFromResource(String resourceName)
        throws IOException, ParserConfigurationException, SAXException {
    InputStream inputStream = HubTestBase.class.getClassLoader().getResourceAsStream(resourceName);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringElementContentWhitespace(true);
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    return builder.parse(inputStream);
}

From source file:org.codice.ddf.security.idp.client.AssertionConsumerServiceTest.java

private static Document parse(String xml) {
    try {//from w  w  w . ja  v  a 2s. c  o  m
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        return documentBuilder.parse(new ByteArrayInputStream(xml.getBytes()));
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}