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:org.opencastproject.remotetest.util.Utils.java

/**
 * Converts a node list to a list of their string values. Nodes that do not have a string value are returned as the
 * empty string.//from www  .j  ava  2s.  c o  m
 */
public static List<String> nodeListToStringList(NodeList nodes) {
    List<String> strings = new ArrayList<String>(nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeValue() != null) {
            strings.add(node.getNodeValue().trim());
        } else {
            Node fchild = node.getFirstChild();
            if (fchild != null && fchild.getNodeValue() != null) {
                strings.add(fchild.getNodeValue().trim());
            } else {
                strings.add("");
            }
        }

    }
    return strings;
}

From source file:Main.java

public static void assertEquivalent(Node node, Node node2) {
    if (node == null) {
        throw new IllegalArgumentException("the first node to be compared is null");
    }//from ww w.j  a v  a2 s  .  c o  m

    if (node2 == null) {
        throw new IllegalArgumentException("the second node to be compared is null");
    }

    if (!node.getNodeName().equals(node2.getNodeName())) {
        throw new IllegalArgumentException("nodes have different node names");
    }

    int attrCount = 0;
    NamedNodeMap attrs = node.getAttributes();
    if (attrs != null) {
        attrCount = attrs.getLength();
    }

    int attrCount2 = 0;
    NamedNodeMap attrs2 = node2.getAttributes();
    if (attrs2 != null) {
        attrCount2 = attrs2.getLength();
    }

    if (attrCount != attrCount2) {
        throw new IllegalArgumentException("nodes hava a different number of attributes");
    }

    outer: for (int i = 0; i < attrCount; i++) {
        Node n = attrs.item(i);
        String name = n.getNodeName();
        String value = n.getNodeValue();

        for (int j = 0; j < attrCount; j++) {
            Node n2 = attrs2.item(j);
            String name2 = n2.getNodeName();
            String value2 = n2.getNodeValue();

            if (name.equals(name2) && value.equals(value2)) {
                continue outer;
            }
        }
        throw new IllegalArgumentException("attribute " + name + "=" + value + " doesn't match");
    }

    boolean hasChildren = node.hasChildNodes();

    if (hasChildren != node2.hasChildNodes()) {
        throw new IllegalArgumentException("one node has children and the other doesn't");
    }

    if (hasChildren) {
        NodeList nl = node.getChildNodes();
        NodeList nl2 = node2.getChildNodes();

        short[] toFilter = new short[] { Node.TEXT_NODE, Node.ATTRIBUTE_NODE, Node.COMMENT_NODE };
        List nodes = filter(nl, toFilter);
        List nodes2 = filter(nl2, toFilter);

        int length = nodes.size();

        if (length != nodes2.size()) {
            throw new IllegalArgumentException("nodes hava a different number of children");
        }

        for (int i = 0; i < length; i++) {
            Node n = (Node) nodes.get(i);
            Node n2 = (Node) nodes2.get(i);
            assertEquivalent(n, n2);
        }
    }
}

From source file:Main.java

public static String text(Node node) {
    StringBuffer sb = new StringBuffer();
    Node n = node.getFirstChild();
    while (n != null) {
        if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE)
            sb.append(n.getNodeValue());
        n = n.getNextSibling();//  w ww  . j  av a2s.c om
    }
    return sb.toString();
}

From source file:Main.java

public static String getElementText(Element e) {
    if (e == null) {
        return ("");
    }//from w  ww  . j a  va2 s.c  om

    NodeList children = e.getChildNodes();

    if (children == null) {
        return ("");
    }

    StringBuffer text = new StringBuffer();

    int listLength = children.getLength();

    for (int i = 0; i < listLength; i++) {
        Node node = children.item(i);

        int nodeType = node.getNodeType();

        if ((nodeType == Node.TEXT_NODE) || (nodeType == Node.CDATA_SECTION_NODE)) {
            String nodeValue = node.getNodeValue();
            if (nodeValue != null) {
                text.append(nodeValue);
            }
        }
    }

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

From source file:Main.java

/** Prints the specified node, then prints all of its children. */

public static void printDOM(Node node) {

    int type = node.getNodeType();

    switch (type) {

    // print the document element
    case Node.DOCUMENT_NODE: {
        System.out.print("<?xml version=\"1.0\" ?>");
        printDOM(((Document) node).getDocumentElement());
        break;/*from w  ww  . j  a  v  a2  s.  c  o m*/
    }

    // print element with attributes
    case Node.ELEMENT_NODE: {
        System.out.println();
        System.out.print("<");
        System.out.print(node.getNodeName());
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            System.out.print(" " + attr.getNodeName().trim() + "=\"" + attr.getNodeValue().trim() + "\"");
        }
        System.out.print(">");
        NodeList children = node.getChildNodes();

        if (children != null) {
            int len = children.getLength();
            for (int i = 0; i < len; i++)
                printDOM(children.item(i));
        }
        break;
    }

    // handle entity reference nodes

    case Node.ENTITY_REFERENCE_NODE: {
        System.out.print("&");
        System.out.print(node.getNodeName().trim());
        System.out.print(";");
        break;
    }

    // print cdata sections
    case Node.CDATA_SECTION_NODE: {
        System.out.print("<![CDATA[");
        System.out.print(node.getNodeValue().trim());
        System.out.print("]]>");
        break;
    }

    // print text
    case Node.TEXT_NODE: {
        System.out.println();
        System.out.print(node.getNodeValue().trim());
        break;
    }

    // print processing instruction

    case Node.PROCESSING_INSTRUCTION_NODE: {
        System.out.print("<?");
        System.out.print(node.getNodeName().trim());
        String data = node.getNodeValue().trim();
        {
            System.out.print(" ");
            System.out.print(data);
        }
        System.out.print("?>");
        break;
    }
    }

    if (type == Node.ELEMENT_NODE) {
        System.out.println();
        System.out.print("</");
        System.out.print(node.getNodeName().trim());
        System.out.print('>');
    }
}

From source file:Main.java

/** Transform xml element and children into a string
 *
 * @param nd Node root of elements to transform
 * @return String representation of xml// w  w w  . j a va 2  s  .c  o m
 */
public static String elementToString(Node nd, boolean add_newlines) {
    //short type = n.getNodeType();

    if (Node.CDATA_SECTION_NODE == nd.getNodeType()) {
        return "<![CDATA[" + nd.getNodeValue() + "]]&gt;";
    }

    // return if simple element type
    final String name = nd.getNodeName();
    if (name.startsWith("#")) {
        if (name.equals("#text"))
            return nd.getNodeValue();
        return "";
    }

    // output name
    String ret = "<" + name;

    // output attributes
    NamedNodeMap attrs = nd.getAttributes();
    if (attrs != null) {
        for (int idx = 0; idx < attrs.getLength(); idx++) {
            Node attr = attrs.item(idx);
            ret += " " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\"";
        }
    }

    final String text = nd.getTextContent();
    final NodeList child_ndls = nd.getChildNodes();
    String all_child_str = "";

    for (int idx = 0; idx < child_ndls.getLength(); idx++) {
        final String child_str = elementToString(child_ndls.item(idx), add_newlines);
        if ((child_str != null) && (child_str.length() > 0)) {
            all_child_str += child_str;
        }
    }
    if (all_child_str.length() > 0) {
        // output children
        ret += ">" + (add_newlines ? "\n" : " ");
        ret += all_child_str;
        ret += "</" + name + ">";
    } else if ((text != null) && (text.length() > 0)) {
        // output text
        ret += text;
        ret += "</" + name + ">";
    } else {
        // output nothing
        ret += "/>" + (add_newlines ? "\n" : " ");
    }

    return ret;
}

From source file:Main.java

public static Object getContent(Element element) {
    NodeList nl = element.getChildNodes();
    StringBuffer content = new StringBuffer();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            return node;
        case Node.CDATA_SECTION_NODE:
        case Node.TEXT_NODE:
            content.append(node.getNodeValue());
            break;
        }/* w  ww  .  ja v a  2s  .  c o m*/
    }
    return content.toString().trim();
}

From source file:com.tascape.qa.th.android.model.UIA.java

public static UIANode parseNode(Node node) {
    if (!node.getNodeName().equals(UIANode.TAG_NAME)) {
        return null;
    }//from  ww  w  . j av  a 2s  .  c  o m

    NamedNodeMap map = node.getAttributes();
    String klass = map.getNamedItem("class").getNodeValue();
    UIANode uiNode = newNode(klass);

    for (int i = 0, j = map.getLength(); i < j; i++) {
        Node attr = map.item(i);
        uiNode.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }

    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        UIANode n = parseNode(nl.item(i));
        if (n == null) {
            continue;
        }
        uiNode.addNode(n);
    }

    return uiNode;
}

From source file:Main.java

static public String getNodeAttr(String tagName, String attrName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            NodeList childNodes = node.getChildNodes();
            for (int y = 0; y < childNodes.getLength(); y++) {
                Node data = childNodes.item(y);
                if (data.getNodeType() == Node.ATTRIBUTE_NODE) {
                    if (data.getNodeName().equalsIgnoreCase(attrName)) {
                        return data.getNodeValue();
                    }//from   ww w. j  av a2s  . co m
                }
            }
        }
    }

    return "";
}

From source file:Main.java

/**
 * Prints attribute variables of the specified node.
 * //from  w w  w.j  a va  2s .com
 * @param node the specified node.
 * @param level a number of indent.
 */
private static void printAttribute(Node node, int level) {
    for (int i = 0; i < level; i++) {
        System.out.print("  ");
    }
    System.out.println("<" + node.getNodeName() + " = " + node.getNodeValue() + ">");
}