Java XML Text Node Create createTextChildElement(Document doc, Node node, String name, String value)

Here you can find the source of createTextChildElement(Document doc, Node node, String name, String value)

Description

create Text Child Element

License

Open Source License

Declaration

public static void createTextChildElement(Document doc, Node node, String name, String value) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2017 Alex Xu and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * //from w w  w  .j av a2s.c o  m
 * Contributors:
 *     Alex Xu - initial API and implementation
 *******************************************************************************/

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static void createTextChildElement(Document doc, Node node, String name, String value) {
        Element element = createChildElement(doc, node, name);
        element.appendChild(doc.createTextNode(value));
    }

    /**
     * Create a child of the given node at the given index.
     *
     * @param doc
     *            a document
     * @param element
     *            an element
     * @param index
     *            an index
     * @param nodeName
     *            a node name
     * @return org.w3c.dom.Element
     */
    public static Element createChildElement(Document doc, Element element, int index, String nodeName) {
        Element element2 = doc.createElement(nodeName);
        try {
            NodeList childList = element.getElementsByTagName(nodeName);
            Node child = childList.item(index);
            element.insertBefore(element2, child);
        } catch (Exception e) {
            element.appendChild(element2);
        }
        return element2;
    }

    /**
     * Create a child of the given node.
     *
     * @param doc
     *            a document
     * @param node
     *            a node
     * @param nodeName
     *            a node name
     * @return org.w3c.dom.Element
     */
    public static Element createChildElement(Document doc, Node node, String nodeName) {
        Element element = doc.createElement(nodeName);
        node.appendChild(element);
        return element;
    }
}

Related

  1. addTextNode(Document XMLDocument, Node rootElement, String tagName, String text)
  2. appendElementChild(Document doc, Element parent, String name)
  3. appendElementChildWithText(Document doc, Element parent, String name, String text)
  4. createText(Document doc, Node parent, String contents)
  5. createText(DocumentFragment fragment)
  6. createTextElement(Document d, String textElementName, String textElementValue)
  7. createTextElement(Document doc, String elementName, String value)
  8. createTextElement(Document doc, String name, String content)
  9. createTextElement(final Document document, final String tagName, final String text)