Here you can find the source of addChildElement(Node parent, String tag, String value)
Parameter | Description |
---|---|
parent | The parent node |
tag | The child element name |
value | Value of text node appended to the Child |
public static Element addChildElement(Node parent, String tag, String value)
//package com.java2s; /*/*from w w w . j a v a2s . c o m*/ * $Id$ * * Copyright 1999-2004 The Apache Software Foundation. * * 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 { /** * Adds a Child element to the parent node. If the 'value' is * not null, a Text node with the same value is appended to the * Child. * @param parent The parent node * @param tag The child element name * @param value Value of text node appended to the Child * @return the newly added Child node. */ public static Element addChildElement(Node parent, String tag, String value) { Document doc = (parent.getNodeType() == Node.DOCUMENT_NODE) ? (Document) parent : parent.getOwnerDocument(); Element child = doc.createElement(tag); if (value != null) child.appendChild(doc.createTextNode(value)); parent.appendChild(child); return child; } }