Java XML Element Get findElementAndSetElseCreateAndSet(Document document, Element parent, String child, boolean value)

Here you can find the source of findElementAndSetElseCreateAndSet(Document document, Element parent, String child, boolean value)

Description

find Element And Set Else Create And Set

License

Apache License

Declaration

public static Element findElementAndSetElseCreateAndSet(Document document, Element parent, String child,
            boolean value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    public static Element findElementAndSetElseCreateAndSet(Document document, Element parent, String child,
            String value) {//from  w w w .ja v  a  2s . com
        NodeList nl = parent.getElementsByTagName(child);
        if (nl.getLength() == 0) {
            parent.appendChild(document.createElement(child));
        }
        Element ret = (Element) parent.getElementsByTagName(child).item(0);
        if (ret.getFirstChild() != null) {
            ret.removeChild(ret.getFirstChild());
        }
        ret.appendChild(document.createTextNode(value));
        return ret;
    }

    public static Element findElementAndSetElseCreateAndSet(Document document, Element parent, String child,
            boolean value) {
        return findElementAndSetElseCreateAndSet(document, parent, child, "" + value);
    }

    public static Element findElementAndSetElseCreateAndSet(Document document, Element parent, String child,
            float value) {
        return findElementAndSetElseCreateAndSet(document, parent, child, "" + value);
    }
}

Related

  1. findElement(Document doc, String elementNS, String elementName, String attrName, String attrValue)
  2. findElement(Document doc, String tagName, Properties props)
  3. findElement(String name, Document doc)
  4. findElementElseCreateAndSet(Document document, Element parent, String child, boolean value)
  5. findElementList(String name, String attrName, String attrValue, Document doc)
  6. findElementOrContainer(Document document, Element parent, String element)
  7. getElement(Document doc, QName elementQName)