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

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

Description

find Element Else Create And Set

License

Apache License

Declaration

public static Element findElementElseCreateAndSet(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 findElementElseCreateAndSet(Document document, Element parent, String child,
            String value) {/*ww  w .  j  a v a2s  .c  o m*/
        Element ret = null;
        NodeList nl = parent.getElementsByTagName(child);
        if (nl.getLength() == 0) {
            parent.appendChild(document.createElement(child));
            ret = (Element) parent.getElementsByTagName(child).item(0);
            ret.appendChild(document.createTextNode(value));
        }
        return ret;
    }

    public static Element findElementElseCreateAndSet(Document document, Element parent, String child,
            boolean value) {
        return findElementElseCreateAndSet(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. findElementAndSetElseCreateAndSet(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)
  8. getElement(Document doc, String path)