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

private static DocumentBuilderFactory createDocumentBuilderFactory() {
    DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
    // Assume default processing is too strict
    f.setValidating(false);/*from   w  w w . j  a v  a 2  s . c o m*/
    f.setIgnoringComments(true);
    f.setNamespaceAware(true);

    return f;
}

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

/**
 * Parses the job instance represented by <code>xml</code> and extracts the job identifier.
 * //from ww  w  . j  a  v  a2 s.  co m
 * @param xml
 *          the job instance
 * @return the job identifier
 * @throws Exception
 *           if parsing fails
 */
public static String getJobId(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.JobUtils.java

/**
 * Parses the job instance represented by <code>xml</code> and extracts the job state.
 * /* www  . j av a2  s. co  m*/
 * @param xml
 *          the job instance
 * @return the job state
 * @throws Exception
 *           if parsing fails
 */
public static String getJobState(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:org.opencastproject.remotetest.util.JobUtils.java

/**
 * Parses the job instance represented by <code>xml</code> and extracts the job type.
 * //from   w w  w .  ja  v a 2s.  c o  m
 * @param xml
 *          the job instance
 * @return the job type
 * @throws Exception
 *           if parsing fails
 */
public static String getJobType(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("type");
}

From source file:XMLUtil.java

public static Document parse(InputSource input, boolean validate, boolean namespaceAware,
        ErrorHandler errorHandler, EntityResolver entityResolver) throws IOException, SAXException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(validate);/*w  w  w  .  ja v a2s  .c om*/
    factory.setNamespaceAware(namespaceAware);
    DocumentBuilder builder = null;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        throw new SAXException(ex);
    }

    if (errorHandler != null) {
        builder.setErrorHandler(errorHandler);
    }

    if (entityResolver != null) {
        builder.setEntityResolver(entityResolver);
    }

    assert input != null : "InputSource cannot be null";

    return builder.parse(input);
}

From source file:eu.planets_project.tb.utils.XCDLParser.java

/**
 * // ww w .  ja va2s  .  co  m
 * @param xcdl
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 * @throws XPathExpressionException
 */
public static List<MeasurementImpl> parseXCDL(String xcdl)
        throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(false);
    DocumentBuilder builder;
    builder = factory.newDocumentBuilder();
    Reader reader = new StringReader(xcdl);
    InputSource inputSource = new InputSource(reader);
    Document xcdlDoc = builder.parse(inputSource);
    return XCDLParser.parseXCDL(xcdlDoc);
}

From source file:eu.planets_project.tb.utils.XCDLParser.java

/**
 * @param read/*from   www.  j  av a  2 s. c  om*/
 * @return
 * @throws ParserConfigurationException 
 * @throws IOException 
 * @throws SAXException 
 * @throws XPathExpressionException 
 */
public static List<MeasurementImpl> parseXCDL(InputStream input)
        throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(false);
    DocumentBuilder builder;
    builder = factory.newDocumentBuilder();
    Reader reader = new InputStreamReader(input);
    InputSource inputSource = new InputSource(reader);
    Document xcdlDoc = builder.parse(inputSource);
    return XCDLParser.parseXCDL(xcdlDoc);
}

From source file:edu.toronto.cs.xcurator.cli.CLIRunner.java

private static void setupDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    builder = builderFactory.newDocumentBuilder();
}

From source file:Main.java

public static Document getXMLDocument(String xmlContent) {

    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    Document xmlDocument = null;//from w  ww .j  av a  2s  .c  om
    builderFactory.setValidating(false);
    builderFactory.setNamespaceAware(true);
    try {
        DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
        try {
            xmlDocument = documentBuilder.parse(new InputSource(new StringReader(xmlContent)));
        } catch (SAXException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
    return xmlDocument;

}

From source file:Main.java

/**
 *
 * @param xmlIS/*from w ww.j a v  a  2s .  c  om*/
 * @return
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 */
public static Document deserializeToDom(InputStream xmlIS)
        throws IOException, ParserConfigurationException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    return builder.parse(xmlIS);
}