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 Vector getElementsByAttribValue(org.w3c.dom.Element element, String attrib, String val) {
    NodeList desElements = element.getElementsByTagName("*");
    Vector selElements = new Vector(desElements.getLength() / 10, 10);

    for (int i = 0; i < desElements.getLength(); i++) {
        org.w3c.dom.Node desElement = desElements.item(i);

        if (desElement.getNodeType() == org.w3c.dom.Element.ELEMENT_NODE) {
            NamedNodeMap attributeNodes = desElement.getAttributes();

            org.w3c.dom.Node selAttribNode = attributeNodes.getNamedItem(attrib);

            if (selAttribNode != null && selAttribNode.getNodeValue().equalsIgnoreCase(val)) {
                selElements.add(desElement);
            }//from   w  w  w .  j a va  2  s  . c  om
        }
    }

    return selElements;
}

From source file:Main.java

public static String getBody(Element element) {
    NodeList nodes = element.getChildNodes();
    if (nodes == null || nodes.getLength() == 0)
        return null;

    Node firstNode = nodes.item(0);
    if (firstNode == null)
        return null;

    return firstNode.getNodeValue();
}

From source file:Util.java

public static String getTextValue(Node node) {
    StringBuffer textValue = new StringBuffer();
    int length = node.getChildNodes().getLength();
    for (int i = 0; i < length; i++) {
        Node c = node.getChildNodes().item(i);
        if (c.getNodeType() == Node.TEXT_NODE) {
            textValue.append(c.getNodeValue());
        }//from  ww w . j ava2 s.c  o m
    }
    return textValue.toString().trim();
}

From source file:Main.java

static String getNamedItemNodeValue(NamedNodeMap attributes, String name, String defvalue) {
    Node namenode = attributes.getNamedItem(name);
    if (namenode == null) {
        return defvalue;
    }//from w ww . j  a  v a  2  s.  c o m
    if (namenode.getNodeValue() == null) {
        return defvalue;
    }
    return namenode.getNodeValue();
}

From source file:Main.java

public static void xmlDebug(Node n) {
    System.err.println("NodeName: '" + n.getNodeName() + "'");
    System.err.println("NodeValue: '" + n.getNodeValue() + "'");
}

From source file:Main.java

/**
 * /*from www .j a v a  2  s .  c  om*/
 * @return one large string with the contents of all TextNodes, or null if
 *         there are non text nodes or no text nodes as children.
 */
public static String getContentsOfTextOnlyNode(Node n) {
    NodeList children = n.getChildNodes();
    if (children.getLength() == 0) {
        return null;
    }

    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node.getNodeType() == Node.TEXT_NODE) {
            sb.append(node.getNodeValue());
        } else {
            return null;
        }
    }

    return sb.toString();
}

From source file:Main.java

/**
 * Gets the date between an opening and closing xml tag. E.g. for
 * <tag>xyz</tag> the string xyz would be returned. If there are multiple
 * nodes with the specified tag name, the data of the first node found will
 * be returned./*from w  w  w  .java 2  s.  c  o  m*/
 * 
 * @param doc
 *            a document.
 * @param tagname
 *            the name of the tag.
 * @return the data of the tag or <code>null</code> if no node with the
 *         specified tag name could be found.
 */
public static String getTagData(Document doc, String tagname) {
    Node node = getNode(doc, tagname);
    if (node != null) {
        Node child = node.getFirstChild();
        if (child != null) {
            return child.getNodeValue();
        }
    }
    return null;
}

From source file:Main.java

public static Vector getElementsByTagAndAttribValue(org.w3c.dom.Element element, String tag, String attrib,
        String val) {
    NodeList desElements = element.getElementsByTagName(tag);
    Vector selElements = new Vector(desElements.getLength() / 10, 10);

    for (int i = 0; i < desElements.getLength(); i++) {
        org.w3c.dom.Node desElement = desElements.item(i);

        if (desElement.getNodeType() == org.w3c.dom.Element.ELEMENT_NODE) {
            NamedNodeMap attributeNodes = desElement.getAttributes();

            org.w3c.dom.Node selAttribNode = attributeNodes.getNamedItem(attrib);

            if (selAttribNode != null && selAttribNode.getNodeValue().equalsIgnoreCase(val)) {
                selElements.add(desElement);
            }//from w  w  w .j  a v a2 s. c  o  m
        }
    }

    return selElements;
}

From source file:Main.java

public static List<String> parseChildNodesAsList(Element current, String name) {

    List<String> result = new ArrayList<String>();

    NodeList nodes = current.getElementsByTagName(name);

    for (int i = 0; i < nodes.getLength(); ++i) {

        Element e = (Element) nodes.item(i);

        Node child = e.getFirstChild();

        if (child == null)
            continue;

        result.add(child.getNodeValue());

    }//from  ww  w.j  a  va  2s  .  c o m

    return result;
}

From source file:Main.java

public static Node duplicate(Node node, Document ownerDocument) {
    if (node instanceof Text)
        return ownerDocument.createTextNode(node.getNodeValue());
    Node newNode = ownerDocument.createElement(node.getNodeName());
    NamedNodeMap attribs = node.getAttributes();
    for (int i = 0; i < attribs.getLength(); i++) {
        Node attrib = attribs.item(i);
        addAttribute(newNode, attrib.getNodeName(), attrib.getNodeValue());
    }/*from w ww .  j  ava 2s .  c o m*/
    for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) {
        Node newN = duplicate(n, ownerDocument);
        newNode.appendChild(newN);
    }
    return newNode;
}