Java XML Element Create findElementElseCreateAndAttribute(Document document, Element parent, String element, String attributeName, String attributeValue)

Here you can find the source of findElementElseCreateAndAttribute(Document document, Element parent, String element, String attributeName, String attributeValue)

Description

find Element Else Create And Attribute

License

Apache License

Declaration

public static Element findElementElseCreateAndAttribute(Document document, Element parent, String element,
            String attributeName, String attributeValue) 

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 findElementElseCreateAndAttribute(Document document, Element parent, String element,
            String attributeName, String attributeValue) {
        NodeList nl = parent.getElementsByTagName(element);
        Element e = null;//ww w . j a va2 s  . c  om

        if (nl.getLength() == 0) {
            parent.appendChild(document.createElement(element));
            e = (Element) parent.getElementsByTagName(element).item(0);
            e.setAttribute(attributeName, attributeValue);
        }

        return e;
    }
}

Related

  1. createText(final Node parent, final String text)
  2. createText(Node parent, String tag, String text)
  3. createText(String sValue, Node nParent)
  4. createTextElement(Element parent, String tagName, String text)
  5. createTextElement2(Element parent, String tagName, String text)
  6. findElementElseCreateAndSetAndAttribute(Document document, Element parent, String element, String value, String attributeName, String attributeValue)