Here you can find the source of createChild(Node node, String elemName)
Node
-object with the specified name
and appends it as a child element to the provided Node
- parameter.
Parameter | Description |
---|---|
elem | the <code>Node</code> to which the newly created <code>Node</code> will be appended to. Not allowed to be <code>null</code>. |
elemName | the name of the to-be-created <code>Element</code>, not allowed to be empty or <code>null</code>. |
public static Element createChild(Node node, String elemName)
//package com.java2s; /*/*from w ww .j ava 2 s .com*/ * This file is part of ROOSSTER. * Copyright 2004, Benjamin Reitzammer <benjamin@roosster.org> * All rights reserved. * * ROOSSTER is free software; you can redistribute it and/or modify * it under the terms of the Artistic License. * * You should have received a copy of the Artistic License * along with ROOSSTER; if not, go to * http://www.opensource.org/licenses/artistic-license.php for details * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { /** * Creates a new <code>Node</code>-object with the specified <code>name</code> * and appends it as a child element to the provided <code>Node</code>- * parameter. * * @param elem the <code>Node</code> to which the newly created * <code>Node</code> will be appended to. Not allowed to be <code>null</code>. * @param elemName the name of the to-be-created <code>Element</code>, not allowed * to be empty or <code>null</code>. * @return the created element * @exception IllegalArgumentException if <code>elem</code> and/ord * <code>elemName</code> are null (or empty in the case of <code>elemName</code>) */ public static Element createChild(Node node, String elemName) { if (node == null || elemName == null || "".equals(elemName)) throw new IllegalArgumentException("Arguments are not allowed to be null or empty"); Document document = null; if (node instanceof Document) { document = (Document) node; } else if (node.getOwnerDocument() != null) { document = node.getOwnerDocument(); } Element newChild = null; if (document != null) { newChild = document.createElement(elemName); node.appendChild(newChild); } return newChild; } }