Example usage for org.w3c.dom Node getNodeValue

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

Introduction

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

Prototype

public String getNodeValue() throws DOMException;

Source Link

Document

The value of this node, depending on its type; see the table above.

Usage

From source file:Main.java

public static String getElementText(Element n) {
    NodeList childNodes = n.getChildNodes();
    String result = new String();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.TEXT_NODE) {
            result += node.getNodeValue();
        }//from www  . j  a  va  2s .c  om
    }
    return result;
}

From source file:Main.java

public static final String getValueOf(Node node) {
    if (node == null) {
        return null;
    } else if (node instanceof Text) {
        return node.getNodeValue().trim();
    } else if (node instanceof Element) {
        ((Element) node).normalize();
        Node temp = node.getFirstChild();
        if (temp != null && (temp instanceof Text))
            return temp.getNodeValue().trim();
        else//ww w. j a v a2  s  . c o  m
            return "";
    } else {
        return node.getNodeValue().trim();
    }
}

From source file:Main.java

public static String getTextForNode(Node node) {
    StringBuilder sb = new StringBuilder();

    NodeList children = node.getChildNodes();
    if (children.getLength() == 0)
        return null;

    for (int i = 0; i < children.getLength(); ++i) {
        Node n = children.item(i);

        if (n instanceof Text)
            sb.append(n.getNodeValue());
        else if (n instanceof EntityReference) {
            String s = getTextForNode(n);
            if (s == null)
                return null;
            else//ww  w  .  j  a v  a  2  s  .  c o  m
                sb.append(s);
        } else
            return null;
    }

    return sb.toString();
}

From source file:Main.java

private static Map<?, ?> getNodeBean(Node parent) {
    Map<Object, Object> rtn = new HashMap<Object, Object>();

    if (parent != null) {
        Map<Object, Object> attrMap = new HashMap<Object, Object>();
        if (parent.hasAttributes()) {
            NamedNodeMap attrs = parent.getAttributes();
            for (int j = 0; j < attrs.getLength(); j++) {
                Node attr = attrs.item(j);
                attr.getNodeName();/*from   w  w  w .j a  v  a2  s  .c o  m*/
                attr.getNodeValue();
                attrMap.put(attr.getNodeName(), attr.getNodeValue());
            }
        }

        rtn.put("tagName", parent.getNodeName());
        rtn.put("attr", attrMap);

        NodeList nodeList = parent.getChildNodes();
        if (nodeList != null) {
            List<Object> children = new ArrayList<Object>();
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node child = nodeList.item(i);
                if (child.getNodeType() == Node.ELEMENT_NODE) {
                    children.add(getNodeBean(child));
                }
            }

            rtn.put("children", children);
        }
    }

    return rtn;
}

From source file:Utils.java

public static String getAttribute(Node element, String attName) {
    NamedNodeMap attrs = element.getAttributes();
    if (attrs == null) {
        return null;
    }/* ww  w .  j  a  v  a 2s .co  m*/
    Node attN = attrs.getNamedItem(attName);
    if (attN == null) {
        return null;
    }
    return attN.getNodeValue();
}

From source file:Main.java

public static String nodeToString(Node node) {
    String ret = "<null/>";
    if (node != null) {
        ret = "<" + node.getNodeName() + " ";
        NamedNodeMap attrs = node.getAttributes();
        if (attrs != null) {
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = (Node) attrs.item(i);
                ret = ret + attr.getNodeName() + "='";
                ret = ret + attr.getNodeValue() + "' ";
            }//from  w  ww  .  j a  v a 2 s .c  om
        }
        ret = ret + "/>";
    }
    return ret;
}

From source file:Utils.java

public static String getElementStringValue(Document document, Element parent, String element) {
    NodeList nl = parent.getElementsByTagName(element);
    if (nl.getLength() == 0) {
        return "";
    }/*from www.  j  av a2  s.  c o m*/

    Node n = nl.item(0).getFirstChild();
    if (n == null) {
        return "";
    }

    return n.getNodeValue();
}

From source file:Main.java

static public String getFragmentText(DocumentFragment elm) {
    Node node = elm.getFirstChild();
    if (node != null && node.getNodeType() == Node.TEXT_NODE)
        return node.getNodeValue();

    return null;/*from   w  w w  .  j a v a  2s  .  c  om*/
}

From source file:Main.java

static public String getElementText(Element elm) {
    Node node = elm.getFirstChild();
    if (node != null && node.getNodeType() == Node.TEXT_NODE)
        return node.getNodeValue();

    return null;//  w w w. jav  a 2  s . c om
}

From source file:Main.java

/**
 * A method to get attribute value from provided xml node and attribute name
 * @param Node parent, a node where the attribute residing
 * @param String attr, attribute name to get
 * @return String , a value from request attribute 
 *//*from   ww  w.  j  a  v  a  2s  .c  o m*/
static public String getAttribute(Node parent, String Attr) {
    String ret = "";

    if (!parent.hasAttributes())
        return ("");
    NamedNodeMap nmap = parent.getAttributes();

    for (int i = 0; i < nmap.getLength(); ++i) {
        Node n = nmap.item(i);
        if (n.getNodeName().trim().equals(Attr)) {
            ret = n.getNodeValue().trim();
        }
        //System.out.println("Attribute: "+n.getNodeType()+" - "+n.getNodeName()+" "+n.getNodeValue());

    }
    return (ret);
}