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

public static Document parseStreamToXML(InputStream in) {
    try {/*from www  .  j  ava 2s  .c om*/
        // Use JAXP to create a document builder
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

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

        return builderFactory.newDocumentBuilder().parse(in);
    } catch (ParserConfigurationException errParser) {
        throw new RuntimeException("Error getting XML parser", errParser);
    } catch (SAXException errSax) {
        throw new RuntimeException("Error parsing XML files", errSax);
    } catch (IOException errIO) {
        throw new RuntimeException("Error parsing XML files", errIO);
    }
}

From source file:Main.java

public static Document parseStreamToXML(Reader in) {
    try {//from w ww.j a v  a2s  .c o  m
        // Use JAXP to create a document builder
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

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

        return builderFactory.newDocumentBuilder().parse(new InputSource(in));
    } catch (ParserConfigurationException errParser) {
        throw new RuntimeException("Error getting XML parser", errParser);
    } catch (SAXException errSax) {
        throw new RuntimeException("Error parsing XML files", errSax);
    } catch (IOException errIO) {
        throw new RuntimeException("Error parsing XML files", errIO);
    }
}

From source file:Main.java

public static Document parse(File file) throws IOException {
    try {/*w w  w .j  a  va  2  s.c  o  m*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setCoalescing(true);
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(true);
        factory.setNamespaceAware(true);
        DocumentBuilder parser = factory.newDocumentBuilder();
        return parser.parse(file);
    } catch (ParserConfigurationException e) {
        throw new IOException(e);
    } catch (SAXException e) {
        throw new IOException(e);
    }
}

From source file:Main.java

public static Document parse(InputStream is) throws IOException {
    try {/*  www. j  a va 2s.c o  m*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setCoalescing(true);
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(true);
        factory.setNamespaceAware(true);
        DocumentBuilder parser = factory.newDocumentBuilder();
        InputSource in = new InputSource(is);
        return parser.parse(in);
    } catch (ParserConfigurationException e) {
        throw new IOException(e);
    } catch (SAXException e) {
        throw new IOException(e);
    }
}

From source file:Main.java

/**
 * //from  ww  w .ja  va 2s  .co m
 */
public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setValidating(false);
    factory.setIgnoringElementContentWhitespace(true);
    factory.setIgnoringComments(true);
    factory.setCoalescing(true);
    factory.setNamespaceAware(false);

    return factory.newDocumentBuilder();
}

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

public static Document parseXml(InputStream in) throws Exception {
    try {/*from   w  w w  . j  ava 2s.  c  o  m*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(in);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:Main.java

public static DocumentBuilderFactory newNamespaceAwareFactory() {
    /*//from   w  w w.j  a v  a 2  s  . c  o  m
     * TODO return DocumentBuilderFactory.newInstance(
     * "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl",
     * XmlHelper.class.getClassLoader());
     */
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    /*
     * Somewhat required for proper JAXB usage... Really? Why I've wrote
     * that? Default value is false and it seems to works like a charm
     * whatever the value.
     */
    factory.setNamespaceAware(true);
    return factory;
}

From source file:com.impetus.ankush.common.utils.NmapUtil.java

/**
 * @throws ParserConfigurationException// w ww  .jav  a  2 s  . c  o  m
 * @throws SAXException
 * @throws IOException
 * @throws XPathExpressionException
 */
private static Map<String, String> getHostIPMapping(String filePath)
        throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
    // loading the XML document from a file
    DocumentBuilderFactory builderfactory = DocumentBuilderFactory.newInstance();
    builderfactory.setNamespaceAware(true);

    DocumentBuilder builder = builderfactory.newDocumentBuilder();
    File file = new File(filePath);
    Document xmlDocument = builder.parse(file);

    XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
    XPath xPath = factory.newXPath();

    // getting the name of the book having an isbn number == ABCD7327923
    XPathExpression hostXpath = xPath.compile("//host");

    XPathExpression ipXpath = xPath.compile("address[@addrtype='ipv4']/@addr");

    XPathExpression hostNameXpath = xPath.compile("hostnames/hostname[@type='user']/@name");

    NodeList nodeListHost = (NodeList) hostXpath.evaluate(xmlDocument, XPathConstants.NODESET);

    Map<String, String> hostIpMapping = new HashMap<String, String>();
    for (int index = 0; index < nodeListHost.getLength(); index++) {
        String ip = (String) ipXpath.evaluate(nodeListHost.item(index), XPathConstants.STRING);
        String host = (String) hostNameXpath.evaluate(nodeListHost.item(index), XPathConstants.STRING);

        hostIpMapping.put(host, ip);

    }
    // deleting the temporary xml file.
    FileUtils.deleteQuietly(file);
    return hostIpMapping;
}

From source file:Main.java

/**
 * Parse the XML file and create Document
 * @param fileName/*from ww w .java 2 s.  c  o  m*/
 * @return Document
 */
public static Document parse(InputStream fs) {
    Document document = null;
    // Initiate DocumentBuilderFactory
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    // To get a validating parser
    factory.setValidating(false);

    // To get one that understands namespaces
    factory.setNamespaceAware(true);
    try {
        // Get DocumentBuilder
        DocumentBuilder builder = factory.newDocumentBuilder();

        // Parse and load into memory the Document
        //document = builder.parse( new File(fileName));
        document = builder.parse(fs);
        return document;
    } catch (SAXParseException spe) {
        // Error generated by the parser
        System.err.println("\n** Parsing error , line " + spe.getLineNumber() + ", uri " + spe.getSystemId());
        System.err.println(" " + spe.getMessage());
        // Use the contained exception, if any
        Exception x = spe;
        if (spe.getException() != null)
            x = spe.getException();
        x.printStackTrace();
    } catch (SAXException sxe) {
        // Error generated during parsing
        Exception x = sxe;
        if (sxe.getException() != null)
            x = sxe.getException();
        x.printStackTrace();
    } catch (ParserConfigurationException pce) {
        // Parser with specified options can't be built
        pce.printStackTrace();
    } catch (IOException ioe) {
        // I/O error
        ioe.printStackTrace();
    }

    return null;
}

From source file:Main.java

/**
 * @param xmlStr//w w  w  .  j  av a2  s . co  m
 * @return the Document or null upon error.
 */
public static Document createDocumentFromXmlString(String xmlStr) {
    try {
        DocumentBuilderFactory docBuilderFactory;
        docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setIgnoringComments(true);
        docBuilderFactory.setIgnoringElementContentWhitespace(true);
        docBuilderFactory.setNamespaceAware(false);
        DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
        ByteArrayInputStream bais = new ByteArrayInputStream(xmlStr.getBytes());
        Document doc = builder.parse(bais);
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}