Here you can find the source of findElementElseCreateAndSetAndAttribute(Document document, Element parent, String element, String value, String attributeName, String attributeValue)
public static Element findElementElseCreateAndSetAndAttribute(Document document, Element parent, String element, String value, String attributeName, String attributeValue)
//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 findElementElseCreateAndSetAndAttribute(Document document, Element parent, String element, String value, String attributeName, String attributeValue) { Element e = findElementElseCreateAndAttribute(document, parent, element, attributeName, attributeValue); if (e != null) e.appendChild(document.createTextNode(value)); return e; }//from ww w . j av a 2s .c o m public static Element findElementElseCreateAndAttribute(Document document, Element parent, String element, String attributeName, String attributeValue) { NodeList nl = parent.getElementsByTagName(element); Element e = null; if (nl.getLength() == 0) { parent.appendChild(document.createElement(element)); e = (Element) parent.getElementsByTagName(element).item(0); e.setAttribute(attributeName, attributeValue); } return e; } }