Android XML Document Create newDocument(String rootName)

Here you can find the source of newDocument(String rootName)

Description

new Document

Declaration

public final static Document newDocument(String rootName)
            throws ParserConfigurationException 

Method Source Code

//package com.java2s;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;

import org.w3c.dom.Node;

public class Main {
    public final static Document newDocument(String rootName)
            throws ParserConfigurationException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document xml = db.newDocument();
        Node root = xml.createElement(rootName);
        Attr a = xml.createAttribute("version");
        a.setValue("0.6");
        root.getAttributes().setNamedItem(a);

        a = xml.createAttribute("encoding");
        a.setValue("UTF-8");
        root.getAttributes().setNamedItem(a);
        xml.appendChild(root);//from   w  w  w .j  a va 2 s.com
        return xml;
    }
}

Related

  1. createEmptyDocument()
  2. createNewDocument()
  3. createDocument()
  4. stringToDocument(String xmlString)
  5. stringToDocument(String xmlString)
  6. createDocument(final String source, boolean isNamespaceAware)
  7. createXml(Document document)