Java XML Element Find getAllNodeNames(Element ele)

Here you can find the source of getAllNodeNames(Element ele)

Description

get All Node Names

License

Open Source License

Parameter

Parameter Description
ele a parameter

Return

array of all child node names. This includes all attribute names as well as all unique child-element names

Declaration

public static String[] getAllNodeNames(Element ele) 

Method Source Code

//package com.java2s;
/*/*from  w w  w.j a  v  a  2  s  .  com*/
 * Copyright (c) 2015 EXILANT Technologies Private Limited (www.exilant.com)
 * Copyright (c) 2016 simplity.org
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.util.HashSet;

import java.util.Set;

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;

import org.w3c.dom.NodeList;

public class Main {
    /**
     * @param ele
     * @return array of all child node names. This includes all attribute names
     *         as well as all unique child-element names
     */
    public static String[] getAllNodeNames(Element ele) {
        NamedNodeMap atts = ele.getAttributes();
        int nbrAtts = atts == null ? 0 : atts.getLength();

        int nbrChildren = 0;
        Set<String> childNames = null;
        NodeList children = ele.getChildNodes();
        if (children != null) {
            nbrChildren = children.getLength();
            childNames = new HashSet<String>();
            for (int i = 0; i < nbrChildren; i++) {
                childNames.add(children.item(i).getNodeName());
            }
            nbrChildren = childNames.size();
        }
        int total = nbrAtts + nbrChildren;
        String[] names = new String[total];
        int idx = 0;
        if (atts != null) {
            for (int i = 0; i < nbrAtts; i++) {
                names[idx] = atts.item(i).getNodeName();
                idx++;
            }
        }
        if (childNames != null) {
            for (String childName : childNames) {
                names[idx] = childName;
                idx++;
            }
        }
        return names;
    }
}

Related

  1. getAllElements(Element config, String elementName)
  2. getAllElements(Element element)
  3. getAllElements(Node context)
  4. getAllElementsByTagName(Element elem, String name)
  5. getAllLeaveValues(Element element)
  6. getAllXmiIds(Element rootElement, String elementName)