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:de.codesourcery.eve.apiclient.utils.XMLParseHelper.java

public static String getChildValue(Element node, String childName) {

    final NodeList l = node.getChildNodes();
    Element result = null;//  w ww  . ja  v  a2 s  .c  o m
    for (int i = 0; i < l.getLength(); i++) {
        final Node n = l.item(i);
        if (n instanceof Element && n.getNodeName().equals(childName)) {
            if (result != null) {
                throw new UnparseableResponseException("Found more than one child element " + " named '"
                        + childName + "' " + "below node '" + node.getNodeName() + "' , expected exactly one");
            }
            result = (Element) n;
        }
    }
    if (result == null) {
        throw new UnparseableResponseException("Expected child element " + " '" + childName + "' "
                + " not found below node '" + node.getNodeName());
    }

    final String value = result.getTextContent();
    if (StringUtils.isBlank(value)) {
        throw new UnparseableResponseException("Child element " + " '" + childName + "' " + " below node '"
                + node.getNodeName() + " has no/blank value ?");
    }
    return value;
}

From source file:com.nexmo.client.verify.endpoints.SearchEndpoint.java

protected static SearchResult.VerifyCheck parseCheckXmlNode(Element root) throws NexmoResponseParseException {
    String code = null;/*from ww  w.j  a  va  2s .c o  m*/
    SearchResult.VerifyCheck.Status status = null;
    Date dateReceived = null;
    String ipAddress = 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 ("code".equals(name)) {
            code = XmlUtil.stringValue(node);
        } else if ("status".equals(name)) {
            String str = XmlUtil.stringValue(node);
            if (str != null) {
                try {
                    status = SearchResult.VerifyCheck.Status.valueOf(str);
                } catch (IllegalArgumentException e) {
                    log.error("xml parser .. invalid value in <status> node [ " + str + " ] ");
                }
            }
        } else if ("ip_address".equals(name)) {
            ipAddress = XmlUtil.stringValue(node);
        } else if ("date_received".equals(name)) {
            String str = XmlUtil.stringValue(node);
            if (str != null) {
                try {
                    dateReceived = parseDateTime(str);
                } catch (ParseException e) {
                    log.error("xml parser .. invalid value in <date_received> node [ " + str + " ] ");
                }
            }
        }
    }

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

    return new SearchResult.VerifyCheck(dateReceived, code, status, ipAddress);
}

From source file:org.zaizi.oauth2utils.OAuthUtils.java

public static void parseXMLDoc(Element element, Document doc, Map<String, String> oauthResponse) {
    NodeList child = null;/* w  ww . j a va  2s .  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) {
            org.w3c.dom.Element childElement = (org.w3c.dom.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

/**
 *
 * @param element/*from   ww w .  ja  va2  s .  c o  m*/
 * @param attributeName
 * @param sequence
 * @param matches
 * @return
 */
private static int addAttributeLoopElement(Element element, String attributeName, int sequence,
        String... matches) {

    int seqNo = sequence;
    if (matches(element.getTagName(), matches)) {
        String seq = "00000000000000000000" + seqNo++;
        seq = seq.substring(seq.length() - 20);
        element.setAttribute(attributeName, attributeName + "_" + seq);
    }
    NodeList nodeList = element.getChildNodes();
    int length = nodeList.getLength();
    for (int seq = 0; seq < length; seq++) {
        Node node = nodeList.item(seq);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            seqNo = addAttributeLoopElement((Element) node, attributeName, seqNo, matches);
        }
    }

    return seqNo;
}

From source file:de.codesourcery.utils.xml.XmlHelper.java

public static List<Element> getNodeChildren(Element parent, String childTag, int minCount, int maxCount)
        throws ParseException {

    final List<Element> result = new LinkedList<Element>();

    if (parent != null) {

        NodeList list = parent.getChildNodes();

        for (int i = 0; i < list.getLength(); i++) {
            Node n = list.item(i);
            if (n.getNodeName().equals(childTag)) {
                if (n instanceof Element) {
                    result.add((Element) n);
                } else {
                    log.trace("getNodeChildren(): Ignoring non-Element " + n.getNodeName() + " , type "
                            + n.getNodeType());
                }/*w w w  . j  a v  a2 s .  c  om*/
            }
        }
    }

    if (result.isEmpty() && minCount > 0) {
        final String msg = "XML is lacking required child tag <" + childTag + "> for parent tag <"
                + parent.getTagName() + ">";
        log.error("getElement(): " + msg);
        throw new ParseException(msg, -1);
    }

    if (maxCount > 0 && result.size() > maxCount) {
        final String msg = "XML may contain at most " + maxCount + " child tags <" + childTag
                + "> for parent tag <" + parent.getTagName() + ">";
        log.error("getElement(): " + msg);
        throw new ParseException(msg, -1);
    }
    return result;
}

From source file:Main.java

private static List<String> findAllBeanIds(Element parent, String tag) {
    List<String> beanIds = new ArrayList<String>();
    // First check to see if any parameters are null
    if (parent == null || tag == null)
        return null;
    // Check to see if this is the element we are interested in. This is
    // redundant apart from first call, but keep in to keep functionality
    //      if (nodeNameEqualTo(parent, tag))
    //         return parent;
    // Get all the children
    NodeList list = parent.getChildNodes();
    int listCount = list.getLength();
    for (int k = 0; k < listCount; k++) {
        Node child = list.item(k);
        // If the node is not an element, ignore it
        if (child.getNodeType() != Node.ELEMENT_NODE)
            continue;
        // Check to see if this node is the node we want
        if (nodeNameEqualTo(child, tag)) {
            beanIds.add(((Element) child).getAttribute("id"));
        }//ww  w .ja  v  a2s  .  com
    }

    return beanIds;
}

From source file:com.nexmo.insight.sdk.NexmoInsightClient.java

private static InsightResult parseInsightResult(Element root) throws IOException {
    String requestId = null;// ww w .  ja va 2s.c  om
    String number = null;
    float price = -1;
    float balance = -1;
    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 ("requestId".equals(name)) {
            requestId = node.getFirstChild() == null ? null : node.getFirstChild().getNodeValue();
        } else if ("number".equals(name)) {
            number = node.getFirstChild() == null ? null : node.getFirstChild().getNodeValue();
        } else if ("status".equals(name)) {
            String str = node.getFirstChild() == null ? null : node.getFirstChild().getNodeValue();
            try {
                if (str != null)
                    status = Integer.parseInt(str);
            } catch (NumberFormatException e) {
                log.error("xml parser .. invalid value in <status> node [ " + str + " ] ");
                status = InsightResult.STATUS_INTERNAL_ERROR;
            }
        } else if ("requestPrice".equals(name)) {
            String str = node.getFirstChild() == null ? null : node.getFirstChild().getNodeValue();
            try {
                if (str != null)
                    price = Float.parseFloat(str);
            } catch (NumberFormatException e) {
                log.error("xml parser .. invalid value in <requestPrice> node [ " + str + " ] ");
            }
        } else if ("remainingBalance".equals(name)) {
            String str = node.getFirstChild() == null ? null : node.getFirstChild().getNodeValue();
            try {
                if (str != null)
                    balance = Float.parseFloat(str);
            } catch (NumberFormatException e) {
                log.error("xml parser .. invalid value in <remainingBalance> node [ " + str + " ] ");
            }
        } else if ("errorText".equals(name)) {
            errorText = node.getFirstChild() == null ? null : node.getFirstChild().getNodeValue();
        }
    }

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

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

    return new InsightResult(status, requestId, number, price, balance, errorText, temporaryError);
}

From source file:com.github.alexfalappa.nbspringboot.projects.initializr.InitializrProjectWizardIterator.java

private static void filterProjectXML(FileObject fo, ZipInputStream str, String name) throws IOException {
    try {/*  w w w  .  ja v  a 2  s  .co m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileUtil.copy(str, baos);
        Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false,
                false, null, null);
        NodeList nl = doc.getDocumentElement().getElementsByTagName("name");
        if (nl != null) {
            for (int i = 0; i < nl.getLength(); i++) {
                Element el = (Element) nl.item(i);
                if (el.getParentNode() != null && "data".equals(el.getParentNode().getNodeName())) {
                    NodeList nl2 = el.getChildNodes();
                    if (nl2.getLength() > 0) {
                        nl2.item(0).setNodeValue(name);
                    }
                    break;
                }
            }
        }
        try (OutputStream out = fo.getOutputStream()) {
            XMLUtil.write(doc, out, "UTF-8");
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
        writeFile(str, fo);
    }

}

From source file:Main.java

private static Element findChildById(Element parent, String tag, String attributeIdValue) {
    // First check to see if any parameters are null
    if (parent == null || tag == null)
        return null;
    // Check to see if this is the element we are interested in. This is
    // redundant apart from first call, but keep in to keep functionality
    //      if (nodeNameEqualTo(parent, tag))
    //         return parent;
    // Get all the children
    NodeList list = parent.getChildNodes();
    int listCount = list.getLength();
    for (int k = 0; k < listCount; k++) {
        Node child = list.item(k);
        // If the node is not an element, ignore it
        if (child.getNodeType() != Node.ELEMENT_NODE)
            continue;
        // Check to see if this node is the node we want
        if (nodeNameEqualTo(child, tag)) {
            if (((Element) child).getAttribute("id").equals(attributeIdValue)) {
                return (Element) child;
            }//from www  . ja  va  2  s  .c om
        }
    }
    // Now that we have checked all children, we can recurse
    for (int k = 0; k < listCount; k++) {
        Node child = list.item(k);
        // If the node is not an element, ignore it
        if (child.getNodeType() != Node.ELEMENT_NODE)
            continue;
        Element result = findChildById((Element) child, tag, attributeIdValue);
        if (result != null)
            return result;
    }
    return null;
}