Java XML Child Get by Name getChildData(Element e, String tag)

Here you can find the source of getChildData(Element e, String tag)

Description

get Child Data

License

Open Source License

Declaration

public static String getChildData(Element e, String tag) 

Method Source Code

//package com.java2s;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static String getChildData(Element e, String tag) {
        NodeList children = e.getElementsByTagName(tag);
        if (children == null || children.getLength() == 0)
            return null;

        return getChildData((Element) children.item(0));
    }//from  ww w .  jav a2s.co  m

    public static String getChildData(Element e) {
        NodeList eChildren = e.getChildNodes();
        if (eChildren == null || eChildren.getLength() == 0)
            return null;

        // OK, now, find the text node and return it's value
        for (int index = 0; index < eChildren.getLength(); index++) {
            Node n = eChildren.item(index);
            if (n.getNodeType() == Node.TEXT_NODE) {
                return n.getNodeValue();
            }
        }
        return null;
    }
}

Related

  1. getChildByTagName(Element e, String name)
  2. getChildByTagName(Element element, String childElementName)
  3. getChildByTagName(Element element, String tag)
  4. getChildByTagName(Element element, String tagName)
  5. getChildByTagName(Node parent, String tagname)
  6. getChildDouble(final Element parent, final String name)
  7. getChildDoubleByName(Node node, String nodeName, double errValue)
  8. getChildElemByTagName(Element parent, String tagName)
  9. getChildElement(Element currentNode, String nsURI, String localName)