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:Main.java

private static List<Node> getNode(Element element, String nodeWeiZhi) {
    List<Node> notes = new ArrayList<Node>();
    String[] nodeNames = nodeWeiZhi.split(">");
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        String nodeName = node.getNodeName();
        if (nodeName.equals(nodeNames[0])) {
            if (nodeNames.length == 1) {
                notes.add(node);/*w w w. j  a  v  a2  s  .  com*/
            }
            String nodeWeiZhiTemp = nodeWeiZhi.replaceFirst(nodeNames[0], "").replaceFirst(">", "");
            NodeList childrenTempList = node.getChildNodes();
            if (childrenTempList.getLength() > 0 && !nodeWeiZhiTemp.equals("")) {
                notes.addAll(getNode((Element) node, nodeWeiZhiTemp));
            }
        }
    }
    return notes;
}

From source file:Main.java

/**
 * Get all child elements with the provided name that are direct children
 * the provided element./*from  w w w.  j  ava 2 s  . co  m*/
 * 
 * @param parent
 *            The parent element
 * @param name
 *            The name of the child elements to find
 * @return The list with child elements, empty list if no matching children
 */
public static Collection<Element> getChildElementsByName(Element parent, String name) {
    assertNotNull(parent);
    Collection<Element> childList = new ArrayList<Element>();

    NodeList children = parent.getChildNodes();
    Node node;
    for (int i = 0; i < children.getLength(); i++) {
        node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) {
            childList.add((Element) node);
        }
    }

    return childList;
}

From source file:Main.java

/**
 * Get all child elements with the provided local name that are direct
 * children the provided element.//from ww w . j  a va2s  .  c  om
 * 
 * @param parent
 *            The parent element
 * @param name
 *            The local name of the child elements to find
 * @return The list with child elements, empty list if no matching children
 */
public static Collection<Element> getChildElementsByLocalName(Element parent, String name) {
    assertNotNull(parent);
    Collection<Element> childList = new ArrayList<Element>();

    NodeList children = parent.getChildNodes();
    Node node;
    for (int i = 0; i < children.getLength(); i++) {
        node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getLocalName().equals(name)) {
            childList.add((Element) node);
        }
    }

    return childList;
}

From source file:Main.java

/**
 * Check if the element contains just a text/cdata, in which case return
 * that value. Else return null;/*from  w ww .  j  a  va2s. co m*/
 *
 * @param element
 * @return null if this is not a textElement as we see it. Value of single
 *         text/CData child otherwise
 */
private static String getElementValue(Element element) {
    if (element.getAttributes().getLength() > 0) {
        return null;
    }

    NodeList children = element.getChildNodes();
    String value = null;
    int nbrChildren = children.getLength();
    for (int i = 0; i < nbrChildren; i++) {
        Node child = children.item(i);
        short childType = child.getNodeType();
        if (childType == Node.ELEMENT_NODE) {
            return null;
        }
        if (childType == Node.CDATA_SECTION_NODE || childType == Node.TEXT_NODE) {
            if (value != null) {
                return null;
            }
            value = child.getNodeValue();
        }
    }
    return value;
}

From source file:Main.java

/**
 * Gets the value of the child element by tag name under the given parent
 * element. If there is more than one child element, return the value of the
 * first one./*www .  ja  va2s  .c  o m*/
 *
 * @param parent the parent element
 * @param tagName the tag name of the child element
 * @return value of the first child element, NULL if tag not exists
 */
public static String getElementValue(Element parent, String tagName) {
    Element element = getChildElement(parent, tagName);
    if (element != null) {
        NodeList nodes = element.getChildNodes();
        if (nodes != null && nodes.getLength() > 0) {
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                if (node instanceof Text) {
                    return ((Text) node).getData();
                }
            }
        }
    }

    return null;
}

From source file:DomUtils.java

/**
 * Extract the text value from the given DOM element, ignoring XML comments.
 * <p>Appends all CharacterData nodes and EntityReference nodes
 * into a single String value, excluding Comment nodes.
 * @see CharacterData/*from   ww  w  .ja v  a 2  s.com*/
 * @see EntityReference
 * @see Comment
 */
public static String getTextValue(Element valueEle) {
    StringBuffer value = new StringBuffer();
    NodeList nl = valueEle.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node item = nl.item(i);
        if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
            value.append(item.getNodeValue());
        }
    }
    return value.toString();
}

From source file:Main.java

/**
 * @param element/*from  ww  w .  j  a va  2 s. c  o  m*/
 * @param map
 * @return
 */
private static boolean fillAttsIntoMap(Element element, Map<String, String> map) {
    /*
     * this case is applicable only if there are no child elements
     */
    if (element.getChildNodes().getLength() > 0) {
        return false;
    }

    /*
     * in case this is not a special case, it will not have attributes, and
     * hence we are safe
     */
    NamedNodeMap attrs = element.getAttributes();
    int n = attrs.getLength();
    for (int i = 0; i < n; i++) {
        Node att = attrs.item(i);
        map.put(att.getNodeName(), att.getNodeValue());
    }
    return true;
}

From source file:Main.java

/**
 * Returns the children nodes of elem with a specific name.
 * /*from w ww  . ja va  2s .  co  m*/
 * @param elem
 *            parent node.
 * @param childName
 *            name of the wanted children.
 * @return list of nodes.
 */
public static List<Element> getChild(Element elem, String childName) {
    NodeList childNodes;
    Node node;
    ArrayList<Element> res;

    res = new ArrayList<Element>();
    childNodes = elem.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        node = childNodes.item(i);
        if (node instanceof Element) {
            if (node.getNodeName() == childName)
                res.add((Element) node);
        }
    }
    return res;
}

From source file:Main.java

public static List<Element> getSubElementByName(Element element, String tagName) {
    ArrayList<Element> result = new ArrayList<Element>();
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        switch (childNodes.item(i).getNodeType()) {
        case Node.ELEMENT_NODE:
            if (((Element) childNodes.item(i)).getTagName().equalsIgnoreCase(tagName))
                result.add((Element) childNodes.item(i));
            break;
        }//w w  w  .j ava  2s  . c o  m
    }
    return result;
}

From source file:Main.java

public static String getPseudoHTML(Element xmlElement) {
    StringBuffer html = new StringBuffer();
    if (xmlElement.hasChildNodes()) {
        NodeList nodes = xmlElement.getChildNodes();
        int children = nodes.getLength();
        for (int i = 0; i < children; i++)
            parsePseudoHTML(nodes.item(i), html);
    }// w w  w . j av a2  s  .  c  o  m
    return html.toString();
}