Java XML Element Child Append appendChildElement(Element parent, Element el, String[] order)

Here you can find the source of appendChildElement(Element parent, Element el, String[] order)

Description

Append a child element to the parent at the specified location.

License

Open Source License

Parameter

Parameter Description
parent parent to which the child will be appended
el element to be added
order order of the elements which must be followed

Exception

Parameter Description
IllegalArgumentException if the order cannot be followed, eithera missing existing or new child element is not specified in order

Declaration

public static void appendChildElement(Element parent, Element el, String[] order)
        throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/*/*  ww w.jav a  2  s  .c  o  m*/
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
 *
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 * Other names may be trademarks of their respective owners.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common
 * Development and Distribution License("CDDL") (collectively, the
 * "License"). You may not use this file except in compliance with the
 * License. You can obtain a copy of the License at
 * http://www.netbeans.org/cddl-gplv2.html
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 * specific language governing permissions and limitations under the
 * License.  When distributing the software, include this License Header
 * Notice in each file and include the License file at
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the GPL Version 2 section of the License file that
 * accompanied this code. If applicable, add the following below the
 * License Header, with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * Contributor(s):
 *
 * The Original Software is NetBeans. The Initial Developer of the Original
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
 * Microsystems, Inc. All Rights Reserved.
 *
 * If you wish your version of this file to be governed by only the CDDL
 * or only the GPL Version 2, indicate your decision by adding
 * "[Contributor] elects to include this software in this distribution
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 * single choice of license, a recipient has the option to distribute
 * your version of this file under either the CDDL, the GPL Version 2 or
 * to extend the choice of license to its licensees as provided above.
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 * Version 2 license, then the option applies only if the new code is
 * made subject to such option by the copyright holder.
 */

import java.util.ArrayList;
import java.util.Arrays;

import java.util.List;

import org.w3c.dom.Element;

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

public class Main {
    /**
     * Append a child element to the parent at the specified location.
     *
     * Starting with a valid document, append an element according to the schema
     * sequence represented by the <code>order</code>.  All existing child elements must be
     * include as well as the new element.  The existing child element following
     * the new child is important, as the element will be 'inserted before', not
     * 'inserted after'.
     *
     * @param parent parent to which the child will be appended
     * @param el element to be added
     * @param order order of the elements which must be followed
     * @throws IllegalArgumentException if the order cannot be followed, either
     * a missing existing or new child element is not specified in order
     *
     * @since 8.4
     */
    public static void appendChildElement(Element parent, Element el, String[] order)
            throws IllegalArgumentException {
        List<String> l = Arrays.asList(order);
        int index = l.indexOf(el.getLocalName());

        // ensure the new new element is contained in the 'order'
        if (index == -1) {
            throw new IllegalArgumentException(
                    "new child element '" + el.getLocalName() + "' not specified in order " + l); // NOI18N
        }

        List<Element> elements = findSubElements(parent);
        Element insertBefore = null;

        for (Element e : elements) {
            int index2 = l.indexOf(e.getLocalName());
            // ensure that all existing elements are in 'order'
            if (index2 == -1) {
                throw new IllegalArgumentException(
                        "Existing child element '" + e.getLocalName() + "' not specified in order " + l); // NOI18N
            }
            if (index2 > index) {
                insertBefore = e;
                break;
            }
        }

        parent.insertBefore(el, insertBefore);
    }

    /**
     * Find all direct child elements of an element.
     * Children which are all-whitespace text nodes or comments are ignored; others cause
     * an exception to be thrown.
     * @param parent a parent element in a DOM tree
     * @return a list of direct child elements (may be empty)
     * @throws IllegalArgumentException if there are non-element children besides whitespace
     * 
     * @since 8.4
     */
    public static List<Element> findSubElements(Element parent) throws IllegalArgumentException {
        NodeList l = parent.getChildNodes();
        List<Element> elements = new ArrayList<Element>(l.getLength());
        for (int i = 0; i < l.getLength(); i++) {
            Node n = l.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                elements.add((Element) n);
            } else if (n.getNodeType() == Node.TEXT_NODE) {
                String text = ((Text) n).getNodeValue();
                if (text.trim().length() > 0) {
                    throw new IllegalArgumentException("non-ws text encountered in " + parent + ": " + text); // NOI18N
                }
            } else if (n.getNodeType() == Node.COMMENT_NODE) {
                // OK, ignore
            } else {
                throw new IllegalArgumentException("unexpected non-element child of " + parent + ": " + n); // NOI18N
            }
        }
        return elements;
    }
}

Related

  1. appendChild(Document doc, Element parentElement, String elementName, String elementValue)
  2. appendChild(Document doc, Element parentElement, String elementName, String elementValue)
  3. appendChild(Document doc, Node parentNode, String childName, String childContents)
  4. appendChild(Document document, Node root, String name, String value)
  5. appendChildElement(Document doc, Element parent, Element child)
  6. appendChildElement(Element parentNode, String nodeName)
  7. appendChildElement(final Element parentElement, final String name, final String value)
  8. appendChildElementBoolean(Element parentNode, String tagName, boolean content)
  9. appendChildElmt(Element aElmt, String aChildName)