Example usage for org.w3c.dom Text getNodeValue

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

Introduction

In this page you can find the example usage for org.w3c.dom Text 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 getNodeText(Node node) {
    if (node != null) {
        Text bb = (Text) node.getChildNodes().item(0);
        return bb.getNodeValue();
    }//from ww w. j a va 2  s.c  om
    return "";
}

From source file:Main.java

public static String getChildNodeText(Node node, String pathname) {
    Node childNode = getChildNode(node, pathname);
    if (childNode != null) {
        Text bb = (Text) childNode.getChildNodes().item(0);
        return bb.getNodeValue();
    }/*  w  ww. ja  v a2s .c om*/
    return "";
}

From source file:Main.java

/**
 * Get the value of this node. Will return "" instead of null.
 * @return java.lang.String/*from w  w w  .  j  av  a2  s  .c  o m*/
 * @param node org.w3c.dom.Node
 */
public static String getNodeValue(Node node) {
    NodeList nodeList = node.getChildNodes();

    int length = nodeList.getLength();
    for (int i = 0; i < length; i++) {
        Node n = nodeList.item(i);
        if (n instanceof Text) {
            Text t = (Text) n;
            return t.getNodeValue();
        }
    }
    return "";
}

From source file:Main.java

/** Returns the String value of the content of the Node specified.
 *  @param Node the node whose content to get.
 *  @return String the value of the content of the Node.  An empty String if the Node
 *  does not have any content.//from  www  .  j a  v  a 2s.  co m
 */
public static String getNodeTextValue(Node pElement) {
    NodeList children = pElement.getChildNodes();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i) instanceof Text) {
            Text n = (Text) children.item(i);
            sb.append(n.getNodeValue());
        }
    }
    String v = sb.toString();
    return v.toString();
}

From source file:Main.java

/**
 * Returns the String value of the content of the Node specified.
 *
 * @param pElement the node whose content to get.
 *
 * @return the value of the content of the Node.  An empty String if the Node
 *         does not have any content.// w  w w. j a  v  a 2  s . c  o m
 */
public static String getNodeTextValue(Node pElement) {
    NodeList children = pElement.getChildNodes();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i) instanceof Text) {
            Text n = (Text) children.item(i);
            sb.append(n.getNodeValue());
        }
    }
    return sb.toString();
}

From source file:Main.java

/**
 * returns the text value associated with the element
 *
 * @param target - the element/*from w  ww .j a  va 2s.c o m*/
 * @return - the text value
 */
public static String getElementValue(final Element target) {

    final NodeList nodeList = target.getChildNodes();
    if (nodeList == null) {
        return null;
    }
    for (int current = 0; current < nodeList.getLength(); current++) {
        final Node node = nodeList.item(current);
        if (node instanceof Text) {
            final Text text = (Text) node;
            final String value = text.getNodeValue();
            if ((value != null) && (value.length() > 0)) {
                return value;
            }
        }
    }
    return "";
}

From source file:Main.java

/**
 * Extract nested text from a node. Currently does not handle coalescing
 * text nodes, CDATA sections, etc.//from   w ww. j a  va2 s  .co  m
 *
 * @param parent a parent element
 * @return the nested text, or null if none was found
 *
 * @since 8.4
 */
public static String findText(Node parent) {
    NodeList l = parent.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        if (l.item(i).getNodeType() == Node.TEXT_NODE) {
            Text text = (Text) l.item(i);
            return text.getNodeValue();
        }
    }
    return null;
}

From source file:Main.java

public static boolean updateTextNode(String nodeName, Element searchFrom, String data, int position)
        throws Exception {
    boolean result = false;
    Element currentElement = (Element) searchFrom.getElementsByTagName(nodeName).item(position);

    if (currentElement != null && currentElement.getNodeType() == Node.ELEMENT_NODE) {
        Text textNode = (Text) (currentElement.getFirstChild());
        textNode.setData(data);/*from  w  w w . java  2  s  .c om*/

        if (textNode != null && textNode.getNodeValue() == null)
            result = false;
        else
            result = true;
    }
    return result;
}

From source file:Main.java

/**
 * Performs the actual recursive dumping of a DOM tree to a given
 * <CODE>PrintStream</CODE>. Note that dump is intended to be a detailed
 * debugging aid rather than pretty to look at. 
 * //from  www.jav  a  2s.c  o  m
 * @param    out            The <CODE>PrintStream</CODE> to write to.
 * @param    node         The <CODE>Node</CODE> under consideration.
 * @param    indent         The level of indentation.
 * @see      #dump(Node)
 * @see      #dump(PrintStream, Node)
 * @since   TFP 1.0
 */
private static void doDump(PrintStream out, final Node node, int indent) {
    if (node != null) {
        for (int index = 0; index < indent; ++index)
            out.write(' ');

        switch (node.getNodeType()) {
        case Node.DOCUMENT_NODE: {
            Document document = (Document) node;

            out.println("DOCUMENT:");

            doDump(out, document.getDoctype(), indent + 1);
            doDump(out, document.getDocumentElement(), indent + 1);
            break;
        }

        case Node.DOCUMENT_TYPE_NODE: {
            DocumentType type = (DocumentType) node;

            out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId="
                    + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]");
            break;
        }

        case Node.ELEMENT_NODE: {
            Element element = (Element) node;

            out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name="
                    + format(element.getLocalName()) + "]");

            NamedNodeMap attrs = element.getAttributes();

            for (int index = 0; index < attrs.getLength(); ++index)
                doDump(out, attrs.item(index), indent + 1);

            for (Node child = element.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }
        case Node.ATTRIBUTE_NODE: {
            Attr attr = (Attr) node;

            out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix="
                    + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value="
                    + format(attr.getNodeValue()) + "]");
            break;
        }

        case Node.TEXT_NODE: {
            Text text = (Text) node;

            out.println("TEXT: [" + format(text.getNodeValue()) + "]");

            for (Node child = text.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }

        case Node.CDATA_SECTION_NODE: {
            CDATASection data = (CDATASection) node;

            out.println("CDATA: [" + format(data.getNodeValue()) + "]");
            break;
        }

        case Node.COMMENT_NODE: {
            Comment comm = (Comment) node;

            out.println("COMMENT: [" + format(comm.getNodeValue()) + "]");
            break;
        }

        default:
            out.println("UNKNOWN: [type=" + node.getNodeType() + "]");
            break;
        }
    }
}

From source file:Main.java

/**
 * Performs the actual recursive dumping of a DOM tree to a given
 * <CODE>PrintWriter</CODE>. Note that dump is intended to be a detailed
 * debugging aid rather than pretty to look at. 
 * //from   ww w. ja v  a 2s  .co  m
 * @param    out            The <CODE>PrintWriter</CODE> to write to.
 * @param    node         The <CODE>Node</CODE> under consideration.
 * @param    indent         The level of indentation.
 * @see      #dump(PrintWriter, Node)
 * @since   TFP 1.0
 */
private static void doDump(PrintWriter out, final Node node, int indent) {
    if (node != null) {
        for (int index = 0; index < indent; ++index)
            out.write(' ');

        switch (node.getNodeType()) {
        case Node.DOCUMENT_NODE: {
            Document document = (Document) node;

            out.println("DOCUMENT:");

            doDump(out, document.getDoctype(), indent + 1);
            doDump(out, document.getDocumentElement(), indent + 1);
            break;
        }

        case Node.DOCUMENT_TYPE_NODE: {
            DocumentType type = (DocumentType) node;

            out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId="
                    + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]");
            break;
        }

        case Node.ELEMENT_NODE: {
            Element element = (Element) node;

            out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name="
                    + format(element.getLocalName()) + "]");

            NamedNodeMap attrs = element.getAttributes();

            for (int index = 0; index < attrs.getLength(); ++index)
                doDump(out, attrs.item(index), indent + 1);

            for (Node child = element.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }
        case Node.ATTRIBUTE_NODE: {
            Attr attr = (Attr) node;

            out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix="
                    + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value="
                    + format(attr.getNodeValue()) + "]");
            break;
        }

        case Node.TEXT_NODE: {
            Text text = (Text) node;

            out.println("TEXT: [" + format(text.getNodeValue()) + "]");

            for (Node child = text.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }

        case Node.CDATA_SECTION_NODE: {
            CDATASection data = (CDATASection) node;

            out.println("CDATA: [" + format(data.getNodeValue()) + "]");
            break;
        }

        case Node.COMMENT_NODE: {
            Comment comm = (Comment) node;

            out.println("COMMENT: [" + format(comm.getNodeValue()) + "]");
            break;
        }

        default:
            out.println("UNKNOWN: [type=" + node.getNodeType() + "]");
            break;
        }
    }
}