Java XML Text Node Create addTextNode(Document XMLDocument, Node rootElement, String tagName, String text)

Here you can find the source of addTextNode(Document XMLDocument, Node rootElement, String tagName, String text)

Description

Adds a simple DOM Node like text to a given node in a document

License

Open Source License

Parameter

Parameter Description
XMLDocument current XML document
rootElement element you want append the new node to
tagName node's tag name
text text in the node

Declaration

public static void addTextNode(Document XMLDocument, Node rootElement, String tagName, String text) 

Method Source Code

//package com.java2s;
/**//w  ww . j av a2s .c om
(C) Copyright 1998-2007 Hewlett-Packard Development Company, LP
    
This library 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; either version 2.1 of the License, or (at your option) any later version.
    
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    
For more information: www.smartfrog.org
*/

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

public class Main {
    /**
     * Adds a simple DOM Node like <tagName>text</tagName> to a given node in a document
     *
     * @param XMLDocument current XML document
     * @param rootElement element you want append the new node to
     * @param tagName node's tag name
     * @param text text in the node
     */
    public static void addTextNode(Document XMLDocument, Node rootElement, String tagName, String text) {
        Element newNode = XMLDocument.createElement(tagName);
        newNode.appendChild(XMLDocument.createTextNode(text));
        rootElement.appendChild(newNode);
    }
}

Related

  1. addTextElement(Document doc, String name, String value, Element parent)
  2. addTextElement(Document document, Node parentNode, String elementName, String elementText)
  3. addTextElement(final Document document, final Element parentDom, final String namespace, final String name, final String value)
  4. addTextElement(final Document document, final Element parentDom, final String namespace, final String name, final String value)
  5. addTextNode(Document document, Element ret, String tag, String value)
  6. appendElementChild(Document doc, Element parent, String name)
  7. appendElementChildWithText(Document doc, Element parent, String name, String text)
  8. createText(Document doc, Node parent, String contents)
  9. createText(DocumentFragment fragment)