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

/**
 * replace the text value in the target/*from   www  .ja  va 2  s .  com*/
 *
 * @param target
 * @param value
 * @return
 */
public static boolean replaceTextValue(final Element target, final String value) {
    if (target == null) {
        return false;
    }
    final NodeList nodeList = target.getChildNodes();
    if (nodeList == null) {
        return false;
    }
    for (int current = 0; current < nodeList.getLength(); current++) {
        final Node node = nodeList.item(current);
        if (node instanceof Text) {
            final Text text = (Text) node;
            text.setData(value);
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static Element getChildWithAttribute(Element element, String attributeName) {

    if (element == null) {
        return null;
    }/*  www .  j a  v a  2  s. c om*/

    NodeList children = element.getChildNodes();

    for (int loop = 0, length = children.getLength(); loop < length; loop++) {
        Node node = children.item(loop);

        if (!(node instanceof Element)) {
            continue;
        }

        Element child = (Element) node;

        if (child.hasAttribute(attributeName)) {
            return child;
        }
    }

    return null;
}

From source file:Main.java

private static String getNodeText(Element element, String nodeWeiZhi) {
    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.getTextContent().trim();
            }/*from  ww  w . java2  s.  c o  m*/
            String nodeWeiZhiTemp = nodeWeiZhi.replaceFirst(nodeNames[0], "").replaceFirst(">", "");
            NodeList childrenTempList = node.getChildNodes();
            if (childrenTempList.getLength() > 0 && !nodeWeiZhiTemp.equals("")) {
                result += getNodeText((Element) node, nodeWeiZhiTemp);
            }
        }
    }
    return result;
}

From source file:Main.java

static public Element selectSingleElement(Element element, String xpathExpression) throws Exception {
    if (xpathExpression.indexOf("/") == -1) {
        NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(xpathExpression)) {
                return (Element) node;
            }/*from   w w  w. j  a va  2  s.co m*/
        }
        //  NodeList nodes = element.getElementsByTagName(xpathExpression);
        //  if (nodes.getLength() > 0) {
        //      return (Element) nodes.item(0);
        //  } else {
        return null;
        //  }
    } else {
        XPath xpath = XPathFactory.newInstance().newXPath();
        Element node = (Element) xpath.evaluate(xpathExpression, element, XPathConstants.NODE);
        return node;
    }
}

From source file:Main.java

public static String GetChildElementText(Element element, String name) {
    if (element != null && name != null && !name.isEmpty()) {
        NodeList nodes = element.getChildNodes();

        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);

            if (node instanceof Element && name.equals(node.getNodeName())) {
                return node.getTextContent();
            }/*from   www  .j a va2s  .c  o m*/
        }
    }
    return null;
}

From source file:Main.java

public static Element getFirstChild(Element paramElement, String paramString1, String paramString2,
        String paramString3) {/*from  w  w  w.j  a va 2  s. c om*/
    NodeList localNodeList = paramElement.getChildNodes();
    for (int i = 0; i < localNodeList.getLength(); i++) {
        Node localNode = localNodeList.item(i);
        if ((localNode != null) && (localNode.getNodeType() == 1)
                && (((Element) localNode).getTagName().equals(paramString1))) {
            Element localElement = (Element) localNode;
            if ((localElement.hasAttribute(paramString2))
                    && (paramString3.equals(localElement.getAttribute(paramString2))))
                return localElement;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Returns first of the <tt>element</tt>'s child nodes that is of type
 * <tt>nodeType</tt>.//from   w  ww  . j a  va2s .  c o m
 * @param element the element whose child we need.
 * @param nodeType the type of the child we need.
 * @return a child of the specified <tt>nodeType</tt> or null if none
 * was found.
 */
public static Node getChildByType(Element element, short nodeType) {
    if (element == null)
        return null;

    NodeList nodes = element.getChildNodes();
    if (nodes == null || nodes.getLength() < 1)
        return null;

    Node node;
    String data;
    for (int i = 0; i < nodes.getLength(); i++) {
        node = nodes.item(i);
        short type = node.getNodeType();
        if (type == nodeType) {
            if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE) {
                data = ((Text) node).getData();
                if (data == null || data.trim().length() < 1)
                    continue;
            }

            return node;
        }
    }

    return null;
}

From source file:Main.java

public static String getTextContent(Element elm) {
    if (elm == null) {
        return null;
    }//  w  ww. java  2s  . co m
    StringBuilder result = new StringBuilder();
    NodeList childNodes = elm.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if ((node == null) || (node.getNodeType() != Node.TEXT_NODE)) {
            continue;
        }
        result.append(((Text) node).getData());
    }
    return result.toString().trim();
}

From source file:Main.java

/**
 * Obtains the list of child element nodes.
 *
 * @param element the root element./*from   w ww  .j a va2s  .  com*/
 * @return the list of child elements.
 */
private static List<Element> getElementList(final Element element) {
    final List<Element> elementList = new ArrayList<Element>();

    final NodeList childNodes = element.getChildNodes();
    final int numChildren = childNodes.getLength();

    for (int i = 0; i < numChildren; i++) {
        final Node childNode = childNodes.item(i);
        if (childNode.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        elementList.add((Element) childNode);
    }

    return elementList;
}

From source file:Main.java

static public String getStringValue(Element el, boolean trimWhitespace) {
    StringBuilder sb = new StringBuilder(1024);
    String str;//from  w  w  w .jav  a2  s  .  c  o m

    NodeList nl = el.getChildNodes();
    for (int i = 0; i < nl.getLength(); ++i) {
        Node n = nl.item(i);
        if (n instanceof Text) {
            sb.append(n.getNodeValue());
        }
    }
    if (trimWhitespace) {
        str = sb.toString().trim();
    } else {
        str = sb.toString();
    }
    return str;

}