Android Utililty Methods XML Document Create

List of utility methods to do XML Document Create

Description

The list of methods to do XML Document Create are organized into topic(s).

Method

DocumentcreateEmptyDocument()
create Empty Document
final DocumentBuilderFactory factory = DocumentBuilderFactory
        .newInstance();
try {
    return factory.newDocumentBuilder().newDocument();
} catch (ParserConfigurationException e) {
    throw new RuntimeException("Should not reach this point");
DocumentcreateNewDocument()
create New Document
final Document document;
DocumentBuilder builder = createDocumentBuilder(false);
document = builder.newDocument();
return document;
DocumentcreateDocument()
create Document
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
        .newDocumentBuilder();
return builder.newDocument();
DocumentnewDocument(String rootName)
new Document
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");
...
DocumentstringToDocument(String xmlString)
Converts String containing XML code to Document
if (xmlString == null)
    return null;
DocumentBuilder documentBuilder = getDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(
        xmlString));
try {
    return documentBuilder.parse(inputSource);
} catch (SAXException e) {
...
DocumentstringToDocument(String xmlString)
Converts String containing XML code to Document
if (xmlString == null)
    return null;
DocumentBuilder documentBuilder = getDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(
        xmlString));
try {
    return documentBuilder.parse(inputSource);
} catch (SAXException e) {
...
DocumentcreateDocument(final String source, boolean isNamespaceAware)
create Document
final Document document;
DocumentBuilder builder = createDocumentBuilder(isNamespaceAware);
try {
    document = builder.parse(new ByteArrayInputStream(source
            .getBytes()));
} catch (SAXException e) {
    String errMsg = "Failed to parse XML source(" + e.getMessage()
            + ") [" + source + "]";
...
StringcreateXml(Document document)
Creates XML from W3C Document from the xml.
TransformerFactory transformerFactory = TransformerFactory
        .newInstance();
Transformer transformer = transformerFactory.newTransformer();
StringWriter stringWriter = new StringWriter();
StreamResult result = new StreamResult(stringWriter);
DOMSource source = new DOMSource(document);
transformer.transform(source, result);
return stringWriter.toString();
...
ElementgetOrCreateElement(Document doc, Element manifestElement, String elementName)
get Or Create Element
NodeList nodeList = manifestElement
        .getElementsByTagName(elementName);
Element element = null;
if (nodeList.getLength() == 0) {
    element = doc.createElement(elementName);
    manifestElement.appendChild(element);
} else {
    element = (Element) nodeList.item(0);
...
DocumentgetDomElement(File xmlFile)
get Dom Element
Document document = null;
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
        .newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory
        .newDocumentBuilder();
InputSource inputSource = new InputSource();
inputSource.setCharacterStream(new FileReader(xmlFile));
document = documentBuilder.parse(inputSource);
...