Example usage for org.w3c.dom Element getChildNodes

List of usage examples for org.w3c.dom Element getChildNodes

Introduction

In this page you can find the example usage for org.w3c.dom Element getChildNodes.

Prototype

public NodeList getChildNodes();

Source Link

Document

A NodeList that contains all children of this node.

Usage

From source file:DomUtil.java

/**
 * Counts the number of immediate child elements of the specified element
 * whose names match the provided <code>name</code> parameter.
 * //from  ww w.  j  a va  2s  .co  m
 * @param parentElement The element to analyze.
 * @param name The name of the child element.
 * @return The number of matching child elements.
 */
public static int getChildElementCountByTagName(Element parentElement, String name) {
    NodeList nodes = parentElement.getChildNodes();
    int length = nodes.getLength();
    int count = 0;
    for (int index = 0; index < length; ++index) {
        if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
                && name.equals(nodes.item(index).getNodeName())) {
            ++count;
        }
    }
    return count;
}

From source file:com.ibm.soatf.tool.ValidateTransferedValues.java

private static boolean isLeafElement(Element e) {
    boolean leaf = true;
    if (e.hasChildNodes()) {
        NodeList list = e.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {
                leaf = false;//from   w  ww  . j a va2  s.c o m
                break;
            }
        }
    }
    return leaf;
}

From source file:com.impetus.kundera.ejb.PersistenceXmlLoader.java

/**
* Find persistence units.//from  w  ww .  j a v  a 2 s  .c  o m
* 
* @param url
*            the url
* @param defaultTransactionType
*            the default transaction type
* @return the list
* @throws Exception
*             the exception
*/
public static List<PersistenceMetadata> findPersistenceUnits(URL url,
        PersistenceUnitTransactionType defaultTransactionType) throws Exception {

    Document doc = getDocument(url);
    Element top = doc.getDocumentElement();
    NodeList children = top.getChildNodes();
    ArrayList<PersistenceMetadata> units = new ArrayList<PersistenceMetadata>();

    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) children.item(i);
            String tag = element.getTagName();
            // look for "persistence-unit" element
            if (tag.equals("persistence-unit")) {
                PersistenceMetadata metadata = parsePersistenceUnit(element);
                units.add(metadata);
            }
        }
    }
    return units;
}

From source file:Main.java

/**
 * If there is no <profiles> section, create one with relevant profile with given profileId
 * If <profiles> already contains a <profile> with given profileId, delete all of its plugins
 * @param document//from  w w w .j  a v a  2s.c o  m
 * @param pomRoot
 * @param profileIdVal
 * @return the <plugins> node (under profiles/profile/build), or null for any error
 */
public static Element initiateProfile(Document document, Element pomRoot, String profileIdVal) {
    //If <profiles> section does not exist, then create and get out
    NodeList childrenRoot = pomRoot.getChildNodes();
    int numProfileNodes = childrenRoot.getLength();
    Node profiles = null;
    for (int index = 0; index < numProfileNodes; index++) {
        Node currNode = childrenRoot.item(index);
        if (currNode.getNodeName().equals(PROFILES_NODE_NAME)) {
            profiles = currNode;
            break;
        }
    }
    if (profiles == null) {
        //no <profiles> section was found.
        //create <profiles>
        Element profilesNew = addProfilesSection(document, pomRoot);

        //Create <profile>, add to <profiles>, and get out
        return addNewProfileSection(document, profilesNew, profileIdVal);
    }

    //the <profiles> section already exists, go over all profiles to determine if the specific profile (with given profileIdVal) exists
    NodeList profileNodes = profiles.getChildNodes();
    numProfileNodes = profileNodes.getLength();
    boolean isProblem = false;
    Element profile = null;
    Element plugins = null;
    for (int profileIndex = 0; profileIndex < numProfileNodes; profileIndex++) {
        //Convert to Element if possible, otherwise it is not relevant
        Node profileNode = profileNodes.item(profileIndex);
        if (!(profileNode instanceof Element)) {
            continue;
        }
        Element currProfile = (Element) profileNode;

        NodeList profileChildren = currProfile.getChildNodes();
        int numProfileChildren = profileChildren.getLength();
        Element profileId = null;
        for (int indexProfileChildren = 0; indexProfileChildren < numProfileChildren; indexProfileChildren++) {
            if (!(profileChildren.item(indexProfileChildren) instanceof Element)) {
                continue;
            }

            if (profileChildren.item(indexProfileChildren).getNodeName().equals(PROFILE_ID_NAME)) {
                profileId = (Element) profileChildren.item(indexProfileChildren);
                break;
            }
        }
        if (profileId == null) {
            //we have a profile without an id, ignore it and move to the next one
            continue;
        }

        //Check if its id is the one we are interested in
        String currProfileIdVal = profileId.getTextContent();
        if (currProfileIdVal.equals(profileIdVal)) {
            profile = currProfile;
            //A profile was found with id = profileId
            //get/create the <build> element
            Element build = getChildElement(document, currProfile, BUILD_NAME);
            if (build == null) {
                isProblem = true;
                break;
            }

            //get/create the <plugins> element
            plugins = getChildElement(document, build, PLUGINS_NAME);
            if (plugins == null) {
                isProblem = true;
                break;
            }

            //remove all children from the <plugins> element (whether or not they are <plugin>'s)
            NodeList pluginNodes = plugins.getChildNodes();
            int numPluginsChildren = pluginNodes.getLength();
            for (int pluginIndex = numPluginsChildren - 1; pluginIndex >= 0; pluginIndex--) {
                plugins.removeChild(pluginNodes.item(pluginIndex));
            }

            //whatever happened above, we are done now, don't check any other profiles
            break;
        }
        //else continue to the next profile
    }

    if (isProblem)
        return null;

    if (profile == null) {
        //the required profile was never found.  Create it now, add to profiles, and get out
        return addNewProfileSection(document, (Element) profiles, profileIdVal);
    } else {
        //we did find the profile, so return the plugins element
        return plugins;
    }
}

From source file:Main.java

/***************************************************************************
 * Returns the value of the first child under the give element with the
 * given name./*from w  w w. j  a v a  2  s .c  o  m*/
 * 
 * @param e
 * @param name
 * @return
 * @throws Exception
 **************************************************************************/
public static String getChildValueByName(Element e, String name) throws Exception {
    String s = "Not found";

    /*
     * The getElementsByTagName() function returns ANY children under the
     * given element with the given tag name. This function is intended to
     * return the value of only an immediate child with a given name.
     */
    NodeList childNodes = e.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        if (node.getNodeName().equals(name)) {
            if (node.getFirstChild() != null)
                s = node.getFirstChild().getNodeValue();
            else
                s = "";
            break;
        }
    }

    return s;
}

From source file:com.bluexml.xforms.actions.EnumAction.java

private static void addValue(Element element, Map<String, String> values, Map<String, String> keys) {
    String id = null;//from  www  .j av a2 s  .c  o  m
    String value = null;

    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if (child instanceof Element) {
            Element e = (Element) child;
            if (StringUtils.equals(e.getTagName(), "id")) {
                id = e.getTextContent();
            }
            if (StringUtils.equals(e.getTagName(), "value")) {
                value = e.getTextContent();
            }
        }
    }
    if (id != null && value != null) {
        values.put(id, value);
        keys.put(value, id);
    }
}

From source file:DomUtil.java

/**
 * Retrieves all immediate child elements of the specified element whose
 * names match the provided <code>name</code> parameter.
 * //from w w  w  . j av  a 2s  .  c  o  m
 * @param parentElement The element to search.
 * @param name The name of the child element.
 * @return An array of matching child elements.
 */
public static Element[] getChildElementsByTagName(Element parentElement, String name) {
    List children = new ArrayList();
    NodeList nodes = parentElement.getChildNodes();
    int length = nodes.getLength();
    for (int index = 0; index < length; ++index) {
        if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
                && name.equals(nodes.item(index).getNodeName())) {
            children.add(nodes.item(index));
        }
    }
    Element[] childElements = new Element[children.size()];
    return (Element[]) children.toArray(childElements);
}

From source file:Main.java

public static List<Element> getElementsByTagName(Element parent, String name, boolean localOnly) {
    List<Element> ret = new ArrayList<Element>();
    if (!localOnly) {
        NodeList elementList = parent.getElementsByTagName(name);
        for (int i = 0; i < elementList.getLength(); i++) {
            ret.add((Element) elementList.item(i));
        }//from www.ja v  a 2 s. c o  m
    } else {
        NodeList childList = parent.getChildNodes();
        for (int i = 0; i < childList.getLength(); i++) {
            if (childList.item(i).getNodeType() != Node.ELEMENT_NODE)
                continue;
            Element child = (Element) childList.item(i);
            if (child.getTagName().equals(name))
                ret.add(child);
        }
    }
    return ret;
}

From source file:XmlHelper.java

/**
 * Returns an iterator over the children of the given element with the given
 * tag name.//from   ww  w  . ja v  a2  s. c  o m
 * 
 * @param element
 *          The parent element
 * @param tagName
 *          The name of the desired child
 * @return An interator of children or null if element is null.
 */
public static Iterator<Element> getChildrenByTagName(Element element, String tagName) {
    if (element == null)
        return null;
    // getElementsByTagName gives the corresponding elements in the whole
    // descendance. We want only children

    NodeList children = element.getChildNodes();
    ArrayList<Element> goodChildren = new ArrayList<Element>();
    for (int i = 0; i < children.getLength(); i++) {
        Node currentChild = children.item(i);
        if (currentChild.getNodeType() == Node.ELEMENT_NODE
                && ((Element) currentChild).getTagName().equals(tagName)) {
            goodChildren.add((Element) currentChild);
        }
    }
    return goodChildren.iterator();
}

From source file:de.akra.idocit.wsdl.services.DocumentationParser.java

/**
 * Returns the name of the thematic grid in the given documentation-element.
 * /*from   w w w .jav  a  2s .c o m*/
 * Please note: This method returns the name of the first thematic grid found in the
 * list of documentation's children.
 * 
 * @param docElemWithThematicGrid
 *            The &lt;documentation&gt;-element containing the thematic grid.
 * 
 * @return The name of the first found thematic grid
 */
public static String readThematicGridName(Element docElemWithThematicGrid) {
    NodeList documentationChildren = docElemWithThematicGrid.getChildNodes();

    int children = documentationChildren.getLength();

    for (int i = 0; i < children; i++) {
        Node child = documentationChildren.item(i);

        if (child.getNodeName().equals(THEMATIC_GRID_TAG_NAME)) {
            NamedNodeMap attributes = child.getAttributes();

            if (attributes != null) {
                Node nameAttribute = attributes.getNamedItem(THEMATIC_GRID_ATTRIBUTE_NAME);

                if (nameAttribute != null) {
                    return nameAttribute.getNodeValue();
                } else {
                    // It seems that we have an incomplete
                    // <thematicgrid>-element. This is why we don't continue
                    // to search for a named one.
                    return null;
                }
            } else {
                // It seems that we have an incomplete
                // <thematicgrid>-element. This is why we don't continue to
                // search for a named one.
                return null;
            }
        }
    }

    return null;
}