Java XML Child Element Add addChildElements(Node n, List elements)

Here you can find the source of addChildElements(Node n, List elements)

Description

add Child Elements

License

Open Source License

Declaration

private static void addChildElements(Node n, List<String> elements) 

Method Source Code

//package com.java2s;
/*/* w w w. jav a 2  s.co m*/
 *  Copyright 2010-2011 the original author or authors.
 *
 *  WebServiceMocker is free software; you can redistribute it and/or modify it under the 
 *  terms of version 2.1 of the GNU Lesser General Public License as published by 
 *  the Free Software Foundation.
 *
 *  WebServiceMocker is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details at gnu.org.
 */

import java.util.List;

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

public class Main {
    private static void addChildElements(Node n, List<String> elements) {

        NodeList nl = n.getChildNodes();

        for (int i = 0; i < nl.getLength(); i++) {

            n = nl.item(i);
            if (n.getNodeType() != 1) {
                continue;
            } else {
                if (hasChild(n)) {
                    addChildElements(n, elements);
                } else {

                    if (!elements.contains(n.getLocalName()))
                        elements.add(n.getLocalName());
                }
            }
        }
    }

    private static boolean hasChild(Node n) {

        NodeList nl = n.getChildNodes();

        for (int i = 0; i < nl.getLength(); i++) {
            n = nl.item(i);
            if (n.getNodeType() == 1)
                return true;
        }
        return false;
    }
}

Related

  1. addChildElement(Element parentElement, String elementName)
  2. addChildElement(Element root, Element element, String name)
  3. addChildElement(Node element, String name, String[][] attributes)
  4. addChildElement(Node parent, String elementName, String textContent)
  5. addChildElement(Node parent, String tag, String value)
  6. addChildNode(Node pNode, String name, String value)
  7. addChildren(Element element, List children)
  8. addElement(Element parent, Element child)
  9. addElement(Element parent, Element childElement)