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

public static Iterator getElementsByTagNames(Element element, String[] tags) {
    List<Element> children = new ArrayList<Element>();
    if (element != null && tags != null) {
        List tagList = Arrays.asList(tags);
        NodeList nodes = element.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node child = nodes.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE && tagList.contains(((Element) child).getTagName())) {
                children.add((Element) child);
            }/*  w  ww. jav  a  2s . c  o m*/
        }
    }
    return children.iterator();
}

From source file:Main.java

/**
 * Returns the child element with the specified tagName for the specified parent element
 * @param parent//w ww.ja  va2  s  .  c  o m
 * @param tagName
 * @return
 */
public static Element getChildElementByTagName(Element parent, String tagName) {
    if (parent == null || tagName == null)
        return null;

    NodeList nodes = parent.getChildNodes();
    Node node;
    int len = nodes.getLength();
    for (int i = 0; i < len; i++) {
        node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && ((Element) node).getNodeName().equals(tagName))
            return (Element) node;
    }

    return null;
}

From source file:Main.java

public static void parseXMLDoc(Element element, Document doc, Map<String, String> oauthResponse) {
    NodeList child = null;/*from   w ww.j ava2  s  .com*/
    if (element == null) {
        child = doc.getChildNodes();

    } else {
        child = element.getChildNodes();
    }
    for (int j = 0; j < child.getLength(); j++) {
        if (child.item(j).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
            Element childElement = (Element) child.item(j);
            if (childElement.hasChildNodes()) {
                System.out.println(childElement.getTagName() + " : " + childElement.getTextContent());
                oauthResponse.put(childElement.getTagName(), childElement.getTextContent());
                parseXMLDoc(childElement, null, oauthResponse);
            }

        }
    }
}

From source file:Main.java

private static String getNodeAttribute(Element element, String nodeWeiZhi, String attributeName) {
    String result = "";
    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) {
                result += "," + node.getAttributes().getNamedItem(attributeName).getNodeValue().trim();
            }/*w  ww. ja  v a  2s .  com*/
            String nodeWeiZhiTemp = nodeWeiZhi.replaceFirst(nodeNames[0], "").replaceFirst(">", "");
            NodeList childrenTempList = node.getChildNodes();
            if (childrenTempList.getLength() > 0 && !nodeWeiZhiTemp.equals("")) {
                result += getNodeAttribute((Element) node, nodeWeiZhiTemp, attributeName);
            }
        }
    }
    return result;
}

From source file:cz.incad.kramerius.rest.api.k5.client.utils.SOLRUtils.java

public static <T> List<T> narray(final Element doc, final String attributeName, Class<T> clz) {
    synchronized (doc.getOwnerDocument()) {
        List<T> ret = new ArrayList<T>();
        List<Element> elms = XMLUtils.getElements(doc, new XMLUtils.ElementsFilter() {
            @Override//from   w w w.j a v a2s . co  m
            public boolean acceptElement(Element element) {
                return (element.getNodeName().equals("arr") && element.hasAttribute("name")
                        && element.getAttribute("name").equals(attributeName));
            }
        });

        if (elms.size() >= 1) {
            Element parentE = elms.get(0);
            NodeList chnds = parentE.getChildNodes();
            for (int i = 0, ll = chnds.getLength(); i < ll; i++) {
                Node n = chnds.item(i);
                if (n.getNodeType() == Node.ELEMENT_NODE) {
                    ret.add(value(n.getTextContent(), clz));
                }
            }
        }
        return ret;
    }
}

From source file:Main.java

/**
 *
 * @param element context element.//from   ww  w.  j a va  2 s  . co m
 * @param namespace namespace of the expected element. Set it to {@code null} if namespace will not be evaluated.
 * @param localname localname of the expected element.
 * @return List of the expected children element. If no match children could be found, empty list will be returned.
 */
public static List<Element> getElementChilden(final Element element, final String namespace,
        final String localname) {
    List<Element> rv = new LinkedList<Element>();

    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (match(child, namespace, localname)) {
            rv.add((Element) child);
        }
    }

    return rv;
}

From source file:Main.java

/**
 * Returns a List of all child Elements of the given Element.
 *
 * @param element the element from which to retrieve child elements
 * @return a List of child Elements./*from  w  w  w .  j  a  v  a2  s .c om*/
 * @throws NullPointerException if the element is null.
 */
public static List getChildElements(Element element) {
    if (element == null)
        throw new NullPointerException("Tried to get child elements on a null element");

    List result = new ArrayList();

    NodeList children = element.getChildNodes();

    for (int i = 0; i != children.getLength(); i++) {
        if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
            result.add(children.item(i));
        }
    }

    return result;
}

From source file:Main.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//  ww  w .  j av  a2s  .c o  m
 * @see EntityReference
 * @see Comment
 */
public static String getTextValue(Element valueEle) {
    if (valueEle == null)
        throw new IllegalArgumentException("Element must not be null");
    StringBuilder sb = new StringBuilder();
    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) {
            sb.append(item.getNodeValue());
        } else if (item instanceof Element) {
            sb.append(getTextValue((Element) item));
        }
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Search for an XML element in the direct children of parent only.
 *
 * This compares localName (nodeName if localName is null) to name, and
 * checks the tags namespace with the provided namespace. A
 * <code>null</code> namespace will match any namespace.
 * <p>/*w ww .  j a v a  2s . c  o  m*/
 * This is differs from the DOM version by:
 * <ul>
 * <li>not searching recursively</li>
 * <li>returns a single result</li>
 * </ul>
 *
 * @param parent    a parent element
 * @param name      the intended local name
 * @param namespace the intended namespace (or null)
 * @return the one child element with that name, or null if none
 * @throws IllegalArgumentException if there is multiple elements of the
 *                                  same name
 *
 * @since 8.4
 */
public static Element findElement(Element parent, String name, String namespace)
        throws IllegalArgumentException {
    Element result = null;
    NodeList l = parent.getChildNodes();
    int nodeCount = l.getLength();
    for (int i = 0; i < nodeCount; i++) {
        if (l.item(i).getNodeType() == Node.ELEMENT_NODE) {
            Node node = l.item(i);
            String localName = node.getLocalName();
            localName = localName == null ? node.getNodeName() : localName;

            if (name.equals(localName) && (namespace == null || namespace.equals(node.getNamespaceURI()))) {
                if (result == null) {
                    result = (Element) node;
                } else {
                    throw new IllegalArgumentException("more than one element with same name found");
                }
            }
        }
    }
    return result;
}

From source file:Main.java

public static Element[] getSubChildElement(Element ele, String[] tagnames) {
    if (ele == null) {
        return null;
    }/* w w w.  j  a v a2 s.  co m*/

    List<Element> v = new ArrayList<Element>();

    NodeList tmpnl = ele.getChildNodes();

    Node tmpn = null;

    int k;
    for (k = 0; k < tmpnl.getLength(); k++) {
        tmpn = tmpnl.item(k);

        if (tmpn.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }

        Element eee = (Element) tmpn;
        String noden = eee.getNodeName();
        int p = noden.indexOf(':');
        if (p >= 0)
            noden = noden.substring(p + 1);

        for (int i = 0; i < tagnames.length; i++) {
            if (tagnames[i].equals(noden) || tagnames[i].equals("*")) {
                v.add(eee);
                break;
            }
        }
    }

    Element[] rets = new Element[v.size()];
    v.toArray(rets);
    return rets;
}