Example usage for org.w3c.dom Node TEXT_NODE

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

Introduction

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

Prototype

short TEXT_NODE

To view the source code for org.w3c.dom Node TEXT_NODE.

Click Source Link

Document

The node is a Text node.

Usage

From source file:Main.java

public static List<Node> getChildNodes(Node dataNode) {
    List<Node> returnList = new ArrayList<Node>();

    NodeList list = dataNode.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() != Node.TEXT_NODE) {
            returnList.add(node);//w ww.ja va  2  s  . com
        }
    }

    return returnList;
}

From source file:Main.java

/**
 * Get first Text node of the Element as String value
 * @param elem/*  w  w w  .j a  va 2 s  .  co m*/
 * @return
 */
public static String getFirstTextValue(Element elem) {
    if (elem != null) {
        Node fc = elem.getFirstChild();
        if (null != fc && fc.getNodeType() == Node.TEXT_NODE) {
            return ((Text) fc).getData();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Get the value of the specified element
 *
 * @param element/*from   w  ww  .  j  ava  2  s  . co m*/
 * @return
 */
public static String getElementValue(Element element) {
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeType() == Node.TEXT_NODE) {
            return element.getFirstChild().getNodeValue();
        }
    }

    return null;
}

From source file:Main.java

public static Object transformXmlNodesIntoMap(NodeList nodes) {
    Map<String, Object> nodeMap = new HashMap<String, Object>();

    if (nodes.getLength() == 1 && nodes.item(0).getNodeType() == Node.TEXT_NODE)
        return nodes.item(0).getTextContent();

    for (int nodeIdx = 0; nodeIdx < nodes.getLength(); nodeIdx++) {
        Node node = nodes.item(nodeIdx);
        NodeList subNodes = node.getChildNodes();

        if (node.getNodeType() != Node.TEXT_NODE)
            nodeMap.put(node.getNodeName(), transformXmlNodesIntoMap(subNodes));
    }/* w  ww  .  j  a  v  a  2s. co  m*/
    return nodeMap;
}

From source file:Main.java

static public void SetNodeText(Node node, String value) {
    NodeList node_;/*from  w w  w . j ava2s.  c  o  m*/
    Node x;
    int n, nnode;
    if (value == null)
        value = "";
    if (node.getNodeType() == Node.TEXT_NODE)
        node.setNodeValue(value);
    else {
        node_ = node.getChildNodes();
        nnode = node_.getLength();
        if (nnode == 0) {
            x = (Node) node.getOwnerDocument().createTextNode(value);
            node.appendChild(x);
        } else {
            for (n = 0; n < nnode; n++) {
                x = (Node) node_.item(n);
                if (x == null)
                    continue;
                if (x.getNodeType() == Node.TEXT_NODE) {
                    x.setNodeValue(value);
                    break;
                }
            }
        }
    }
}

From source file:Main.java

public static Object transformXmlNodesIntoMap(NodeList nodes) {
    Map<String, Object> nodeMap = new HashMap<String, Object>();

    if (nodes.getLength() == 1 && nodes.item(0).getNodeType() == Node.TEXT_NODE)
        // return nodes.item(0).getTextContent();
        return nodes.item(0).getNodeValue();

    for (int nodeIdx = 0; nodeIdx < nodes.getLength(); nodeIdx++) {
        Node node = nodes.item(nodeIdx);
        NodeList subNodes = node.getChildNodes();

        if (node.getNodeType() != Node.TEXT_NODE)
            nodeMap.put(node.getNodeName(), transformXmlNodesIntoMap(subNodes));
    }/*from   w w w . j a va2  s. c o  m*/
    return nodeMap;
}

From source file:Main.java

/**
 *   Gets the text for a node by concatenating child TEXT elements.
 *//* w w  w .j  a va2  s  .  c  o m*/
public synchronized static String getText(Node n) {
    if (n == null)
        throw new IllegalArgumentException("Node argument cannot be null");

    StringBuilder b = new StringBuilder();
    NodeList nl = n.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeType() == Node.TEXT_NODE)
            b.append(nl.item(i).getNodeValue());
        else if (nl.item(i).getNodeType() == Node.CDATA_SECTION_NODE)
            b.append(nl.item(i).getNodeValue());
    }

    return b.toString().trim();
}

From source file:Utils.java

public static String getTextNodeByNumber(Node parent, int number) {
    String text = null;//from ww w.  j ava  2  s  . c  o  m
    int count = 1;

    if (parent != null) {
        for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {

            if ((child.getNodeType() == Node.TEXT_NODE) && (count++ == number)) {
                text = child.getNodeValue();
                return text.trim();
            }
        }
    }
    return text;
}

From source file:Main.java

public static String getTextContent(Node e) {
    if (e == null || e.getNodeType() != Node.ELEMENT_NODE) {
        return null;
    }//from  w w  w. java2  s .c  om

    NodeList nodes = e.getChildNodes();
    StringBuilder text = new StringBuilder();

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

        if (node != null && node.getNodeType() == Node.TEXT_NODE) {
            String s = node.getNodeValue();
            if (s != null) {
                text.append(s);
            }
        }
    }

    if (text.length() > 0) {
        return text.toString();
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Gets the next comment.//from   w w  w  .  j  ava2 s.c o  m
 * 
 * @param element
 *            the element
 * @return the next comment
 */
public static String getNextComment(Node element) {
    while (element.getNextSibling() != null) {
        Node prev = element.getNextSibling();
        if (prev.getNodeType() == Node.COMMENT_NODE) {
            return prev.getTextContent();
        } else if (prev.getNodeType() == Node.TEXT_NODE) {
            return getNextComment(prev);
        } else if (prev.getNodeType() == Node.ELEMENT_NODE) {
            return null;
        }
    }
    return null;
}