Java XML Child Element Add addChildNode(Node pNode, String name, String value)

Here you can find the source of addChildNode(Node pNode, String name, String value)

Description

add Child Node

License

Apache License

Declaration

public static Node addChildNode(Node pNode, String name, String value) 

Method Source Code

//package com.java2s;
/**/*  ww  w  . ja v a  2  s  .  co m*/
 * Copyright 2010 
 *
 * 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 {

    public static Node addChildNode(Node pNode, String name, String value) {
        Document _doc = null;
        if (pNode.getNodeType() == Node.DOCUMENT_NODE) {
            _doc = (Document) pNode;
        } else {
            _doc = pNode.getOwnerDocument();
        }

        Element _ele = _doc.createElement(name);
        Node _node = _doc.createTextNode(value);
        _ele.appendChild(_node);

        return pNode.appendChild(_ele);
    }

    public String getNodeType(Node node) {
        String type;

        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE: {
            type = "Element";
            break;
        }
        case Node.ATTRIBUTE_NODE: {
            type = "Attribute";
            break;
        }
        case Node.TEXT_NODE: {
            type = "Text";
            break;
        }
        case Node.CDATA_SECTION_NODE: {
            type = "CData section";
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            type = "Entity reference";
            break;
        }
        case Node.ENTITY_NODE: {
            type = "Entity";
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            type = "Processing instruction";
            break;
        }
        case Node.COMMENT_NODE: {
            type = "Comment";
            break;
        }
        case Node.DOCUMENT_NODE: {
            type = "Document";
            break;
        }
        case Node.DOCUMENT_TYPE_NODE: {
            type = "Document type";
            break;
        }
        case Node.DOCUMENT_FRAGMENT_NODE: {
            type = "Document fragment";
            break;
        }
        case Node.NOTATION_NODE: {
            type = "Notation";
            break;
        }
        default: {
            type = "???";
            break;
        }
        }
        return type;
    }
}

Related

  1. addChildElement(Element root, Element element, String name)
  2. addChildElement(Node element, String name, String[][] attributes)
  3. addChildElement(Node parent, String elementName, String textContent)
  4. addChildElement(Node parent, String tag, String value)
  5. addChildElements(Node n, List elements)
  6. addChildren(Element element, List children)
  7. addElement(Element parent, Element child)
  8. addElement(Element parent, Element childElement)
  9. addElementChildTo(Element parent, String nsURI, String qualifiedName)