Java XML Child Element Create addElementWithText(Document document, Element parentElement, String newElementName, String textString)

Here you can find the source of addElementWithText(Document document, Element parentElement, String newElementName, String textString)

Description

Creates a new element, with the given text inside that element.

License

Open Source License

Parameter

Parameter Description
document the document into which the element is added.
parentElement The parent element of the element-to-be-added.
newElementName name of new element (tag-name).
textString the text to be written inside the newly created element.

Return

The new element.

Declaration

public static Element addElementWithText(Document document,
        Element parentElement, String newElementName, String textString) 

Method Source Code

//package com.java2s;
/**/*from  www  .  j  a va  2s .  c  o  m*/
 * Copyright 2015 Yahoo Inc.<br>
 * Licensed under the terms of the MIT license. Please see LICENSE file at the root of this project for terms.
 * <p/>
 *
 * @author yuvalp@yahoo-inc.com
 * 
 */

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

public class Main {
    /**
     * Creates a new element, with the given text inside that element.
     * 
     * @param document the document into which the element is added.
     * @param parentElement The parent element of the element-to-be-added.
     * @param newElementName name of new element (tag-name).
     * @param textString the text to be written inside the newly created element.
     * @return The new element.
     */
    public static Element addElementWithText(Document document,
            Element parentElement, String newElementName, String textString) {
        Element newElement = document.createElement(newElementName);
        Text text = document.createTextNode(textString);
        newElement.appendChild(text);
        parentElement.appendChild(newElement);
        return newElement;
    }
}

Related

  1. addElement(final Document document, final Element parentDom, final String namespace, final String name)
  2. addElement(final Document document, final Element root, final String elementValue, final String elementName)
  3. addElement(final Document dom, final Element el, final String name, final String value)
  4. addElementAndSetContent(Document doc, String name, Element parent, String text)
  5. addElementChildToElement(Document doc, Element parent, String elementName)
  6. appendDateNode(Document owner, Element appendElement, String name, Date date)
  7. createAndAppendChild(Element parentElement, String elementName, Properties attributes)
  8. createChild(Document document, Node parent, String childNodeName)
  9. createChild(Element el, String name, boolean attach)