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:com.nexmo.client.verify.endpoints.SharedParsers.java

protected static VerifyResult parseVerifyResponseXmlNode(Element root) throws NexmoResponseParseException {
    String requestId = null;/*from w  w w . ja v  a 2s .  co m*/
    int status = -1;
    String errorText = null;

    NodeList fields = root.getChildNodes();
    for (int i = 0; i < fields.getLength(); i++) {
        Node node = fields.item(i);
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        String name = node.getNodeName();
        if ("request_id".equals(name)) {
            requestId = XmlUtil.stringValue(node);
        } else if ("status".equals(name)) {
            String str = XmlUtil.stringValue(node);
            try {
                if (str != null)
                    status = Integer.parseInt(str);
            } catch (NumberFormatException e) {
                log.error("xml parser .. invalid value in <status> node [ " + str + " ] ");
                status = BaseResult.STATUS_INTERNAL_ERROR;
            }
        } else if ("error_text".equals(name)) {
            errorText = XmlUtil.stringValue(node);
        }
    }

    if (status == -1)
        throw new NexmoResponseParseException("Xml Parser - did not find a <status> node");

    // Is this a temporary error ?
    boolean temporaryError = (status == BaseResult.STATUS_THROTTLED
            || status == BaseResult.STATUS_INTERNAL_ERROR);

    return new VerifyResult(status, requestId, errorText, temporaryError);
}

From source file:Main.java

public static List<Element> elements(final Element element, final Set<String> allowedTagNames) {
    List<Element> elements = null;
    final NodeList nodeList = element.getChildNodes();
    if (nodeList != null) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            final Node child = nodeList.item(i);
            if (Element.class.isAssignableFrom(child.getClass())) {
                final Element childElement = (Element) child;
                final String childTagName = getTagLocalName(childElement);
                if (allowedTagNames.contains(childTagName)) {
                    if (elements == null) {
                        elements = new ArrayList<Element>();
                    }//from ww  w .ja v  a  2 s .c  o  m
                    elements.add(childElement);
                }
            }
        }
    }
    return elements;
}

From source file:com.tascape.qa.th.android.model.UIA.java

public static WindowHierarchy parseHierarchy(InputStream in, UiAutomatorDevice device)
        throws SAXException, ParserConfigurationException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(in);

    Element doc = document.getDocumentElement();
    NodeList nl = doc.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);//from   w ww  .  ja va 2  s  . com
        UIANode uiNode = parseNode(node);
        if (uiNode != null) {
            WindowHierarchy hierarchy = new WindowHierarchy(uiNode);
            hierarchy.setRotation(doc.getAttribute("rotation"));
            LOG.debug("{}", hierarchy);
            hierarchy.setUiAutomatorDevice(device);
            return hierarchy;
        }
    }
    throw new UIAException("Cannot parse view hierarchy");
}

From source file:Main.java

/**
 * Looks through all child elements of the specified root (recursively) and
 * returns the elements that corresponds to all parameters.
 *
 * @param root the Element where the search should begin
 * @param tagName the name of the node we're looking for
 * @param keyAttributeName the name of an attribute that the node has to
 *            have/* w  ww .j av a2s.  com*/
 * @param keyAttributeValue the value that attribute must have
 * @return list of Elements in the tree under root that match the specified
 *         parameters.
 * @throws NullPointerException if any of the arguments is null.
 */
public static List<Element> locateElements(Element root, String tagName, String keyAttributeName,
        String keyAttributeValue) {
    List<Element> result = new ArrayList<Element>();
    NodeList nodes = root.getChildNodes();
    Node node;
    int len = nodes.getLength();
    for (int i = 0; i < len; i++) {
        node = nodes.item(i);
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        // is this the node we're looking for?
        if (node.getNodeName().equals(tagName)) {
            Element element = (Element) node;
            String attr = element.getAttribute(keyAttributeName);

            if (attr != null && attr.equals(keyAttributeValue))
                result.add(element);
        }

        // look inside.

        List<Element> childs = locateElements((Element) node, tagName, keyAttributeName, keyAttributeValue);

        if (childs != null)
            result.addAll(childs);

    }

    return result;
}

From source file:com.weibo.wesync.notify.xml.DomUtils.java

/**
 * Retrieve all child elements of the given DOM element   
        //from  w  w  w.  j  a va2s . c om
 * @param ele         the DOM element to analyze
 * @return a List of child <code>org.w3c.dom.Element</code> instances
 */
public static List<Element> getChildElements(Element ele) {
    Assert.notNull(ele, "Element must not be null");
    NodeList nl = ele.getChildNodes();
    List<Element> childEles = new ArrayList<Element>();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element) {
            childEles.add((Element) node);
        }
    }
    return childEles;
}

From source file:Main.java

/**
 * Get the content of the given element.
 *
 * @param element       The element to get the content for.
 * @param defaultStr    The default to return when there is no content.
 * @return              The content of the element or the default.
 *///from   w  ww  . jav a 2s .c  o  m
public static String getElementContent(Element element, String defaultStr) throws Exception {
    if (element == null)
        return defaultStr;

    NodeList children = element.getChildNodes();
    String result = "";
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i).getNodeType() == Node.TEXT_NODE
                || children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) {
            result += children.item(i).getNodeValue();
        } else if (children.item(i).getNodeType() == Node.COMMENT_NODE) {
            // Ignore comment nodes
        }
    }
    return result.trim();
}

From source file:Main.java

/**
 * This method will find all the parameters under this <code>paramsElement</code> and return them as
 * Map<String, String>. For example,
 * <pre>// w w  w . j a v  a  2s  .com
 *   <result ... >
 *      <param name="param1">value1</param>
 *      <param name="param2">value2</param>
 *      <param name="param3">value3</param>
 *   </result>
 * </pre>
 * will returns a Map<String, String> with the following key, value pairs :-
 * <ul>
 *  <li>param1 - value1</li>
 *  <li>param2 - value2</li>
 *  <li>param3 - value3</li>
 * </ul>
 *
 * @param paramsElement
 * @return
 */
public static Map getParams(Element paramsElement) {
    LinkedHashMap params = new LinkedHashMap();

    if (paramsElement == null) {
        return params;
    }

    NodeList childNodes = paramsElement.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);

        if ((childNode.getNodeType() == Node.ELEMENT_NODE) && "param".equals(childNode.getNodeName())) {
            Element paramElement = (Element) childNode;
            String paramName = paramElement.getAttribute("name");

            String val = getContent(paramElement);
            if (val.length() > 0) {
                params.put(paramName, val);
            }
        }
    }
    return params;
}

From source file:Main.java

/**
 * Returns an iterator over the children of the given element with
 * the given tag name./*from  www .  j  a v a  2 s.com*/
 * @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<>();
    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:Main.java

/**
 * Get textvalue from element. Loop all #text chidlnoes.
 * This is used by getText() methods, endusers usually
 * should not call this directly.//from  ww  w .ja  v a  2  s .c o  m
 */
private static String getSimpleText(Element element) {
    if (element == null)
        return null;
    StringBuilder sb = new StringBuilder();
    NodeList nodes = element.getChildNodes();
    Node node;
    for (int i = 0; i < nodes.getLength(); i++) {
        node = nodes.item(i);
        if (node.getNodeType() == Node.TEXT_NODE)
            sb.append(node.getNodeValue());
    }
    return sb.toString().trim();
}

From source file:Main.java

public static List<Element> getChildNodes(Element ele) {

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

    if (ele == null) {
        return elements;
    }/*from w w  w  . j  a  v a 2s  .  co  m*/

    NodeList nl = ele.getChildNodes();

    if (nl != null && nl.getLength() > 0) {
        for (Integer i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (node instanceof Element) {
                elements.add((Element) node);
            }
        }
    }

    return elements;
}