Java XML Text Node Create addTextElement(Document document, Node parentNode, String elementName, String elementText)

Here you can find the source of addTextElement(Document document, Node parentNode, String elementName, String elementText)

Description

Convenience method for adding an XML text element.

License

Apache License

Parameter

Parameter Description
document the document where the parent resides, and where the new text element will reside. The document may or may not be the parent node itself.
parentNode the parent node of the new text element.
key the name of the element.
value the string within the element.

Declaration

public static void addTextElement(Document document, Node parentNode, String elementName, String elementText) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2013-2014 Esri/*from   ww w  .j a  v  a  2 s.  c o  m*/
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 ******************************************************************************/

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

public class Main {
    /**
     * Convenience method for adding an XML text element. For example, if you call
     * the method like this:<br/>
     * <br/>
     * <code>Utilities.addTextElement(parent, "lastName", "Lockwood");</code><br/>
     * <br/>
     * The new node, when rendered as a string, will look like this:<br/>
     * <br/>
     * <code>&lt;lastName&gt;Lockwood&lt;/lastName&gt;
     * @param document the document where the parent resides, and where the new text
     *                 element will reside. The document may or may not be the parent
     *                 node itself.
     * @param parentNode the parent node of the new text element.
     * @param key the name of the element.
     * @param value the string within the element.
     */
    public static void addTextElement(Document document, Node parentNode, String elementName, String elementText) {
        Element textElement = document.createElement(elementName);
        textElement.appendChild(document.createTextNode(elementText));
        parentNode.appendChild(textElement);
    }
}

Related

  1. addText(Document doc, Node node, String text)
  2. addText(Document doc, Node parent, String data)
  3. addTextElement(Document doc, String name, String value, Element parent)
  4. addTextElement(final Document document, final Element parentDom, final String namespace, final String name, final String value)
  5. addTextElement(final Document document, final Element parentDom, final String namespace, final String name, final String value)
  6. addTextNode(Document document, Element ret, String tag, String value)
  7. addTextNode(Document XMLDocument, Node rootElement, String tagName, String text)