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

/**
 * Returns a <code>DocumentBuilder</code>, which is used for parsing XML
 * documents.//  w  w  w.j  a  v  a  2  s  .  com
 * 
 * @return a <code>DocumentBuilder</code> which is used for parsing XML
 *         documents. Never <code>null</code>.
 */
public static DocumentBuilder getDocumentBuilder() {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setValidating(false);

        return dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage());
    }
}

From source file:Main.java

public static Document load(String file) {
    try {//w w w .  j av a  2 s  . com
        Document document = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(false);
        dbf.setNamespaceAware(false);
        DocumentBuilder builder = dbf.newDocumentBuilder();
        document = builder.parse(new File(file));
        document.normalize();
        return document;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Valida un documento XML con un esquema XML (XSD).
 * @param xml File to validate/* w ww.j  a  v  a2 s .  c o  m*/
 * @param xmlSchema File with the schema
 * @return Dom document
 * @throws ParserConfigurationException 
 * @throws SAXException 
 * @throws IOException  
 */
public static Document validate(File xml, File xmlSchema)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setNamespaceAware(true);
    factory.setValidating(true);
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xmlSchema);

    // Parsing 
    DocumentBuilder documentBuilder = factory.newDocumentBuilder();
    documentBuilder.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException ex) throws SAXException {
            throw ex;
        }

        public void error(SAXParseException ex) throws SAXException {
            throw ex;
        }

        public void fatalError(SAXParseException ex) throws SAXException {
            throw ex;
        }
    });

    return documentBuilder.parse(xml);
}

From source file:Main.java

/** 
 * Return a Node corresponding to the input XML string representation.
 * @param inputSource source.//from  w ww  .ja va  2 s .c  o  m
 * @return An instance of Node corresponding to the input XML string representation.
 * @throws ParserConfigurationException 
 * @throws SAXException 
 * @throws IOException 
 */
public static Node getXMLNode(InputSource inputSource)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(inputSource);
    return (doc);
}

From source file:Main.java

public static DocumentBuilder createParser() {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    try {/* w  w  w.  jav a  2  s  .  c  o  m*/
        dbf.setNamespaceAware(true);
        dbf.setValidating(false);
        dbf.setFeature("http://xml.org/sax/features/namespaces", false);
        dbf.setFeature("http://xml.org/sax/features/validation", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    } catch (ParserConfigurationException ex) {
        throw new RuntimeException(ex);
    }

    try {
        return dbf.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        System.err.println("[error] XmlUtils: Cannot create a DocumentBuilder instance.");
    }

    return null;
}

From source file:Main.java

public static boolean isXMLWellFormed(String xmlUrl)
        throws IOException, SAXException, ParserConfigurationException {

    boolean isXMLValid = false;

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);/*  w ww.  j  a v a  2 s  .  c o  m*/
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = (Document) builder.parse(new InputSource(xmlUrl));
    isXMLValid = true;

    return isXMLValid;
}

From source file:Main.java

public static Document stringToDoc(String srcXmlString) {
    Document doc = null;/*from w w w.  j  a  v  a 2s  .  c  o m*/
    try {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(false); // never forget this!
        DocumentBuilder builder = null;
        builder = domFactory.newDocumentBuilder();
        doc = builder.parse(new ByteArrayInputStream(srcXmlString.getBytes()));
    } catch (Exception ex) {
        logger.error(ex);
    }
    return doc;
}

From source file:Main.java

/**
 * Revalida um objeto Document /* w w w  .  j  a v a 2 s . co m*/
 * @param documento
 * @return Document
 * @throws ParserConfigurationException 
 * @throws IOException 
 * @throws SAXException 
 * @throws TransformerFactoryConfigurationError 
 * @throws TransformerException 
 * @throws TransformerConfigurationException 
 */
public static Document revalidaDocument(Document documento)
        throws ParserConfigurationException, SAXException, IOException, TransformerConfigurationException,
        TransformerException, TransformerFactoryConfigurationError {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document doc = null;
    DocumentBuilder db;
    db = dbf.newDocumentBuilder();
    InputSource is = converterDoc(documento);
    if (is != null) {
        doc = db.parse(is);
    }
    return doc;
}

From source file:Main.java

/**
 * Convert the byte array representation of an Element into an Element
 * @param byte[] representation of an Element.
 * @param boolean set whether the return Element should be namespace aware.
 * @return Element/*from w  w  w .j av a2s.co  m*/
 */
public static Element byteArrayToElement(byte[] b, boolean namespaceAware)
        throws ParserConfigurationException, SAXException, UnsupportedEncodingException, IOException {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware(namespaceAware);
    docBuilderFactory.setValidating(false);
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(new ByteArrayInputStream(b));
    return doc.getDocumentElement();
}

From source file:Main.java

public static boolean validateWithDTDUsingDOM(String xml) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from w w w. j  av  a  2 s .co m*/
    factory.setNamespaceAware(true);

    DocumentBuilder builder = factory.newDocumentBuilder();

    builder.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException e) throws SAXException {
            System.out.println("WARNING : " + e.getMessage()); // do nothing
        }

        public void error(SAXParseException e) throws SAXException {
            System.out.println("ERROR : " + e.getMessage());
            throw e;
        }

        public void fatalError(SAXParseException e) throws SAXException {
            System.out.println("FATAL : " + e.getMessage());
            throw e;
        }
    });
    builder.parse(new InputSource(xml));
    return true;
}