Here you can find the source of findElementAndSetElseCreateAndSet(Document document, Element parent, String child, boolean value)
public static Element findElementAndSetElseCreateAndSet(Document document, Element parent, String child, boolean value)
//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); } }