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 getDefaultNamespaceUri(URL xmlResource, String rootElementName)
        throws IOException, ParserConfigurationException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);//from  w w  w.  jav  a2 s .  c o m
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(xmlResource.openStream());
    NodeList nodes = document.getElementsByTagName(rootElementName);
    if (nodes == null || nodes.getLength() == 0) {
        throw new IllegalArgumentException("Root element \"" + rootElementName + "\" not found in xml \""
                + xmlResource.toExternalForm() + "\".");
    }
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        Node xmlns = node.getAttributes().getNamedItem("xmlns");
        if (xmlns != null) {
            String value = xmlns.getNodeValue();
            return value.substring(value.indexOf("=") + 1);
        }
    }
    return null;
}

From source file:Main.java

public static void printAttributes(Element element) {
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node node = attributes.item(i);
        System.out.println("## prefix=" + node.getPrefix() + " localname:" + node.getLocalName() + " value="
                + node.getNodeValue());
    }/*from w ww.j  a  v a 2  s. c om*/
}

From source file:Main.java

/**
 * Get element node string value./*from   w  w w  . ja v a2  s  .c o m*/
 * 
 * @param element element to get string value for
 * @return concatenated text node descendant values
 */
public static String getStringValue(final Element element) {
    final StringBuilder buf = new StringBuilder();
    final NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        final Node n = children.item(i);
        switch (n.getNodeType()) {
        case Node.TEXT_NODE:
            buf.append(n.getNodeValue());
            break;
        case Node.ELEMENT_NODE:
            buf.append(getStringValue((Element) n));
            break;
        }
    }
    return buf.toString();
}

From source file:Main.java

public static String getNodeValue(Node node) {
    NodeList childNodes = node.getChildNodes();
    for (int x = 0; x < childNodes.getLength(); x++) {
        Node data = childNodes.item(x);
        if (data.getNodeType() == Node.TEXT_NODE)
            return data.getNodeValue();
    }/*from   w ww.ja v a 2s  . c o  m*/
    return "";
}

From source file:Main.java

/**
 * Returns formatted attributes of the node.
 * /*  w  w  w  .  j  a  v a2s.  c  om*/
 * @param node The node.
 * @return Formatted attributes.
 */
public static String formatAttributes(Node node) {
    StringBuilder sb = new StringBuilder();
    NamedNodeMap attrs = node.getAttributes();

    for (int i = 0; i < attrs.getLength(); i++) {
        Node attr = attrs.item(i);
        sb.append(' ').append(attr.getNodeName()).append("= '").append(attr.getNodeValue()).append("'");
    }

    return sb.toString();
}

From source file:Utils.java

/**
 * Get the trimmed text content of a node or null if there is no text
 *///from  w ww  .  j av a 2 s. co m
public static String getContent(Node n) {
    if (n == null) {
        return null;
    }
    Node n1 = getChild(n, Node.TEXT_NODE);
    if (n1 == null) {
        return null;
    }
    return n1.getNodeValue().trim();
}

From source file:Main.java

static public String getDocumentComment(Document doc) {
    String docComment = null;//from   w  ww .j av  a2  s  .  c o m
    try {
        NodeList childs = doc.getChildNodes();
        for (int i = 0; i < childs.getLength(); i++) {
            Node child = childs.item(i);
            if (child.getNodeType() == Node.COMMENT_NODE) {
                docComment = child.getNodeValue();
            }
        }
        return docComment;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String getElementText(Element elem) {
    StringBuilder buf = new StringBuilder();
    NodeList nodeList = elem.getChildNodes();
    for (int i = 0, len = nodeList.getLength(); i < len; i++) {
        Node n = nodeList.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            buf.append(n.getNodeValue());
        } else {/*from   w  ww. j a v a  2s.co m*/
            //TODO see jsf-samples
            //throw new FacesException("Unexpected node type " + n.getNodeType());
        }
    }
    return buf.toString();
}

From source file:Main.java

public static String getText(Element paramElement) {
    NodeList localNodeList = paramElement.getChildNodes();
    int i = localNodeList.getLength();
    for (int j = 0; j < i; ++j) {
        Node localNode = localNodeList.item(j);
        if (localNode.getNodeType() == 3)
            return localNode.getNodeValue();
    }/* ww  w .  ja va 2s  .c om*/
    return "";
}

From source file:XMLUtils.java

/**
 * Returns the value of the given node./*from  w  w w  . jav a  2s . c o  m*/
 * @param base the element from where to search.
 * @param name of the element to get.
 * @return the value of this element.
 */
public static String getStringValueElement(final Element base, final String name) {
    String value = null;

    // Get element
    NodeList list = base.getElementsByTagName(name);
    if (list.getLength() == 1) {
        Element element = (Element) list.item(0);
        Node node = element.getFirstChild();
        if (node != null) {
            value = node.getNodeValue();
        }
    } else if (list.getLength() > 1) {
        throw new IllegalStateException("Element '" + name + "' on '" + base
                + "' should be unique but there are '" + list.getLength() + "' elements");
    }

    if (value != null) {
        value = value.trim();
    }
    return value;
}