Java XML Attribute Append appendBooleanElement(Element parentElement, String nodeName, boolean value)

Here you can find the source of appendBooleanElement(Element parentElement, String nodeName, boolean value)

Description

append Boolean Element

License

Open Source License

Declaration

public static void appendBooleanElement(Element parentElement, String nodeName, boolean value) 

Method Source Code

//package com.java2s;
/**//from  w w  w. j  av a 2 s  .co  m
 * This program is free software:  you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2011 GrossCommerce
 */

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    public static void appendBooleanElement(Element parentElement, String nodeName, boolean value) {
        appendStringElement(parentElement, nodeName, String.valueOf(value));
    }

    public static void appendStringElement(Element parentElement, String nodeName, String nodeValue) {
        // check for exists
        Element elem = selectSingleElement(parentElement, nodeName);

        if (elem == null) {
            elem = parentElement.getOwnerDocument().createElement(nodeName);
            parentElement.appendChild(elem);
        }

        elem.setTextContent(nodeValue);
    }

    public static Element selectSingleElement(Element parent, String elementName) {
        NodeList list = parent.getElementsByTagName(elementName);

        if (list.getLength() > 0) {
            return (Element) list.item(0);
        }

        return null;
    }
}

Related

  1. appendAttribute(Node node, String name, String value)
  2. appendAttributeIfNotNull(Element parentElement, String attributeName, Object attributeValue)
  3. appendAttributeNode(String namespace, Element parent, String name, String value)
  4. appendAttributes(Node node, StringBuffer sb)
  5. appendBooleanAttribute(Element thisElement, String attrName, boolean value)