Example usage for org.w3c.dom Node getFirstChild

List of usage examples for org.w3c.dom Node getFirstChild

Introduction

In this page you can find the example usage for org.w3c.dom Node getFirstChild.

Prototype

public Node getFirstChild();

Source Link

Document

The first child of this node.

Usage

From source file:Main.java

/** count number of child elements with defined uri and localname */
static public int coutElementNS(Node parent, String uri, String localName) {
    int n = 0;/*from  w w  w .j av  a  2 s  .  co  m*/
    for (Node c = parent.getFirstChild(); c != null; c = c.getNextSibling()) {
        n += (isA(c, uri, localName) ? 1 : 0);
    }
    return n;
}

From source file:Main.java

/**
 * Get XML <code>Node</code> text content.  This method duplicates the
 * org.w3c.dom.Node.getTextContent() method in JDK 1.5.
 *
 * @param baseNode The XML node from which the content is being retrieved.
 * @return The text content of the XML node.
 */// w w  w .  j av a 2 s.c  o m
public static String getTextContent(Node baseNode) {
    // if element, first child will be a text element with content
    Node child = baseNode.getFirstChild();
    if (child != null && child.getNodeType() == Node.TEXT_NODE) {
        return child.getNodeValue();
    }
    return "";
}

From source file:Main.java

/**
 * Get the text from the given node. This assumes the node contains a single child that is a text
 * node./*from w ww  .j av a  2 s.  c o m*/
 *
 * @param node the node from which to extract the text
 * @return the raw text, or the empty string if the element has no children
 * @throws IllegalArgumentException if the first child is not a text node
 */
public static String getText(Node node) throws IllegalArgumentException {
    Node text = node.getFirstChild();
    if (text == null)
        return "";
    if (!(text instanceof Text))
        throw new IllegalArgumentException("Expected text, but found node '" + text.getNodeName() + "'");
    return ((Text) text).getData();
}

From source file:Main.java

public static String getTextData(Node node) {
    if (!node.hasChildNodes()) {
        return null;
    }// w  w w  .  j a va  2  s  .  co m
    Node child = node.getFirstChild();
    while (child != null && child.getNodeType() != Node.TEXT_NODE
            && child.getNodeType() != Node.CDATA_SECTION_NODE) {
        child = child.getNextSibling();
    }
    if (child == null) {
        return null;
    }

    if (child.getNodeType() == Node.TEXT_NODE) {
        return ((Text) child).getData();
    } else {
        return ((CDATASection) child).getData();
    }

}

From source file:Main.java

public static Vector<Element> getChildElemsByName(String name, Node parent) {
    Vector<Element> v = new Vector<Element>();
    Element E = null;/*from   ww w .  j  a v a 2  s . c  o m*/
    for (Node childNode = parent.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) {
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (childNode.getNodeName() == name) {
                E = (Element) childNode;
                v.add(E);
            }
        }
    }
    return v;
}

From source file:Main.java

public static String getFirstMatchedValueByChildTagName(Node parent, String name) {
    //List<Element> nodeList = new ArrayList<Element>();
    for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
        //System.out.println(child.getNodeName());
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            if (child.getNodeName().indexOf(name) != -1) {
                return child.getTextContent();
            } else {
                String value = getFirstMatchedValueByChildTagName((Element) child, name);
                if (value != null) {
                    return value;
                }/* w  w  w .ja  v a2  s. co  m*/
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Extracts the inner text value from a xml node accept : <node>value</node> and return value
 * string/*from   w  w  w.j  av  a2  s .c o m*/
 * 
 * @param node XML node to extract inner text from
 * @return innerText if its not empty otherwise null.
 */
private static String getNodeTextValue(Node node) {
    String innerText = null;
    if (node != null) {
        Node textNode = node.getFirstChild();
        if (textNode != null && textNode.getNodeType() == Node.TEXT_NODE) {
            innerText = textNode.getNodeValue();
            innerText = innerText.trim();
            if (innerText.length() == 0) {
                innerText = null;
            }
        }
    }
    return innerText;
}

From source file:Main.java

/**
 *
 * @param node Node// w  w w .ja  v a 2s.com
 * @param nodeName String
 * @param errValue String
 * @return String
 */
public static String getChildStringByName(Node node, String nodeName, String errValue) {
    Node nd = getChildByName(node, nodeName);
    if (nd != null) {
        Node fnd = nd.getFirstChild();
        if (fnd != null)
            return fnd.getNodeValue();
    }
    return errValue;
}

From source file:Main.java

/**
 * Delete children elements for Node//w w  w .  ja va2 s  . c o  m
 */
public static void removeAllChildren(Node node) {
    if (node != null) {
        while (node.hasChildNodes()) {
            node.removeChild(node.getFirstChild());
        }
    }
}

From source file:Main.java

/**
 * get the value of an Element in the Xml Document.
 * finds the first occurance of the parent element and then searches its children
 * for the first occurance of the element and retrieves its value.
 * @param root the root Element.//from   www. j  av  a  2 s.  co m
 * @param parent the name of the parent element to search for.
 * @param elemName the name of the child element to search for.
 * @return String the element value or null if not found.
 */
public static String getSubElementValue(Element root, String parent, String elemName) {
    NodeList nl = root.getElementsByTagName(parent);
    String value = null;
    if (null == nl) {
        return (null);
    }
    Node node = nl.item(0);
    nl = node.getChildNodes();
    if (null == nl) {
        return (null);
    }

    boolean found = false;
    for (int i = 0; !found && i < nl.getLength(); ++i) {
        Node n = nl.item(i);
        if (elemName.equals(n.getNodeName())) {
            value = n.getFirstChild().getNodeValue().trim();
            found = true;
            break;
        } // if
    } // for
    return (value);
}