get Or Create XML Element - Android XML

Android examples for XML:XML Element

Description

get Or Create XML Element

Demo Code


//package com.java2s;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    public static Element getOrCreateElement(Document doc,
            Element manifestElement, String elementName) {
        NodeList nodeList = manifestElement
                .getElementsByTagName(elementName);
        Element element = null;/*from  w  w  w.ja  v a  2s. c o  m*/
        if (nodeList.getLength() == 0) {
            element = doc.createElement(elementName);
            manifestElement.appendChild(element);
        } else {
            element = (Element) nodeList.item(0);
        }
        return element;
    }
}

Related Tutorials