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 void indentXmlFile(File file) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
    Document doc = documentBuilder.parse(file);
    OutputFormat format = new OutputFormat(doc);
    format.setLineWidth(80);/* ww w  . j a v a  2  s .c  o  m*/
    format.setIndenting(true);
    format.setIndent(4);
    XMLSerializer serializer = new XMLSerializer(new FileWriter(file), format);
    serializer.serialize(doc);
}

From source file:Main.java

public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
    if (builder == null) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        builder = factory.newDocumentBuilder();
    }/*from   w w  w. j a v  a2  s .c o m*/
    return builder;
}

From source file:Main.java

public static Document buildNamespaceAwareDocument(File xml)
        throws SAXException, ParserConfigurationException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    return factory.newDocumentBuilder().parse(xml);
}

From source file:Main.java

public static Document getDocument(InputStream source)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true); // never forget this!

    Document doc = domFactory.newDocumentBuilder().parse(source);
    return doc;//w  w w.  ja va 2  s  .  com
}

From source file:Main.java

public static Document newDocument() {
    try {/*from  ww w  .ja v  a 2 s  .  co m*/
        DocumentBuilderFactory xmlFactory = DocumentBuilderFactory.newInstance();
        xmlFactory.setNamespaceAware(true);
        DocumentBuilder builder = xmlFactory.newDocumentBuilder();

        return builder.newDocument();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/** get a namespace aware builder
 * @return properly set up {@link DocumentBuilder}
 *//*from  w  ww  .  j  a  v a 2  s . c o  m*/
public static DocumentBuilder documentBuilder() throws ParserConfigurationException {
    final DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    dbfac.setNamespaceAware(true);
    final DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
    return docBuilder;
}

From source file:Main.java

private static DocumentBuilderFactory createDocumentBuilderFactory() {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setCoalescing(true);//from   w w w  .j  ava2 s  .  c  o m
    factory.setIgnoringComments(true);
    return factory;
}

From source file:Main.java

public static Document deserialize(Object object) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();

    JAXBContext context = JAXBContext.newInstance(object.getClass());
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(object, doc);/*from   w w w .jav  a2 s .com*/
    return doc;
}

From source file:Main.java

public static Document createXMLDocument() {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);
    DocumentBuilder docBuilder = null;
    try {//from   ww  w.  ja v  a 2 s.c  om
        docBuilder = docFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Document doc = docBuilder.newDocument();
    return doc;
}

From source file:Main.java

private static Document getDocument(String xml) throws ParserConfigurationException, SAXException, IOException {
    // Create a builder factory
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);

    return factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
}