Java XML Duration Add appendStringElement(Element parentElement, String nodeName, String nodeValue)

Here you can find the source of appendStringElement(Element parentElement, String nodeName, String nodeValue)

Description

append String Element

License

Open Source License

Declaration

public static void appendStringElement(Element parentElement, String nodeName, String nodeValue) 

Method Source Code

//package com.java2s;
/**//from w  w w .  j  a v a 2s. c  om
 * 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 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. appendElement(Element parent, String type)
  2. appendElement(Node parent, String name)
  3. appendElement(Node parent, String name)
  4. appendElement(Node parent, String tagName)
  5. appendElementNS(Element parent, String namespaceURI, String localName)
  6. appendText(Element parent, String content)
  7. appendTextElement(Element parent, String element, long value)
  8. appendTextElement(Element parent, String name, String content)
  9. appendTextNode(Element parentElement, String initialTextContent)