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 getAttr(Node node, String name) throws DOMException {

    assert node != null;

    try {/*from   w  w w . ja  va  2 s.  c o m*/
        NamedNodeMap attributes = node.getAttributes();
        if (attributes == null)
            throw new Exception("");

        Node attr = attributes.getNamedItem(name);
        if (attr == null)
            throw new Exception("");

        String value = attr.getNodeValue();
        if (value == null)
            throw new Exception("");

        return value;

    } catch (Exception e) {
        throw new DOMException(DOMException.NOT_FOUND_ERR,
                String.format("attribute %s is missing at node %s", name, node.getNodeName()));
    }

}

From source file:Main.java

public static String convertDOMToString(Node node) {
    StringBuffer sb = new StringBuffer();
    if (node.getNodeType() == Node.TEXT_NODE) {
        sb.append(node.getNodeValue());
    } else {//w  w w . j  ava 2 s. com
        String currentTag = node.getNodeName();
        sb.append('<');
        sb.append(currentTag);
        appendAttributes(node, sb);
        sb.append('>');
        if (node.getNodeValue() != null) {
            sb.append(node.getNodeValue());
        }

        appendChildren(node, sb);

        appendEndTag(sb, currentTag);
    }
    return sb.toString();
}

From source file:Main.java

/**
 * This method will return the content of this particular <code>element</code>.
 * For example,//www .  j a  v a 2s.  c  om
 * <p/>
 * <pre>
 *    <result>something_1</result>
 * </pre>
 * When the {@link org.w3c.dom.Element} <code>&lt;result&gt;</code> is passed in as
 * argument (<code>element</code> to this method, it returns the content of it,
 * namely, <code>something_1</code> in the example above.
 *
 * @return
 */
public static String getContent(Element element) {
    StringBuilder paramValue = new StringBuilder();
    NodeList childNodes = element.getChildNodes();
    for (int j = 0; j < childNodes.getLength(); j++) {
        Node currentNode = childNodes.item(j);
        if (currentNode != null && currentNode.getNodeType() == Node.TEXT_NODE) {
            String val = currentNode.getNodeValue();
            if (val != null) {
                paramValue.append(val.trim());
            }
        }
    }
    return paramValue.toString().trim();
}

From source file:Main.java

public static String getChildNodeValue(String nodeName, Node parent) {

    if (parent == null) {
        return null;
    }//  ww w  .j  av a 2 s . com

    NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equals(nodeName)) {
                Node firstChild = node.getFirstChild();
                if ((firstChild != null) && (firstChild.getNodeType() == Node.TEXT_NODE)) {
                    return firstChild.getNodeValue();
                } else {
                    return null;
                }
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * Output a DOM node including children using a log4j logger.
 * If logger is null it will just output to system out.
 * It will indent by the number of tabs passed in.
 * This method recursively calls itself to output
 * children nodes.//from ww  w  .ja v a 2  s. c  o  m
 * 
 * @param logger
 * @param n
 * @param tabs
 */
public static void outputNode(Logger logger, Node n, int tabs) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < tabs; i++) {
        sb.append("\t");
    }
    sb.append("<" + n.getNodeName());

    if (n.hasAttributes()) {
        NamedNodeMap nnMap = n.getAttributes();
        for (int i = 0; i < nnMap.getLength(); i++) {
            Node att = nnMap.item(i);
            sb.append(" " + att.getNodeName() + "=\"" + att.getNodeValue() + "\"");
        }
    }
    sb.append(">");
    sb = printBuffer(logger, sb, true);

    for (int i = 0; i < tabs + 1; i++) {
        sb.append("\t");
    }
    sb.append(n.getNodeValue());
    sb = printBuffer(logger, sb, true);

    if (n.hasChildNodes()) {
        NodeList nodes = n.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            outputNode(nodes.item(i), tabs + 1);
        }
    }
    for (int i = 0; i < tabs; i++) {
        sb.append("\t");
    }
    sb.append("</" + n.getNodeName() + ">");
    sb = printBuffer(logger, sb, true);
}

From source file:Main.java

/**
 * Are elements equal./*w  w w .j  a va2s.  c  o  m*/
 * 
 * @param element1
 *            the element1
 * @param element2
 *            the element2
 * @return true, if successful
 */
public static boolean areElementsEqual(Element element1, Element element2) {
    if (!element1.getTagName().equals(element2.getTagName())) {
        return false;
    }
    NamedNodeMap nodeAttrMap = element1.getAttributes();
    NamedNodeMap pathAttrMap = element2.getAttributes();
    if ((nodeAttrMap == null && pathAttrMap == null)
            || (pathAttrMap.getLength() == 0 && nodeAttrMap.getLength() == 0)) {
        return true;
    } else {
        if (element1.hasAttribute("name") && element2.hasAttribute("name")) {
            if (element1.getAttribute("name").equals(element2.getAttribute("name"))) {
                return true;
            }
        } else if (nodeAttrMap != null && pathAttrMap != null
                && (nodeAttrMap.getLength() == pathAttrMap.getLength())) {
            for (int k = 0; k < nodeAttrMap.getLength(); k++) {
                Node nodeAttr = nodeAttrMap.item(k);
                String nodeAttrName = nodeAttr.getNodeName();
                String nodeAttrValue = nodeAttr.getNodeValue();
                if (element2.hasAttribute(nodeAttrName)
                        && nodeAttrValue.equals(element2.getAttribute(nodeAttrName))) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:XMLUtils.java

/**
 * Returns the value of the child node with the given name.
 * @param base the element from where to search.
 * @param name of the element to get.//from  ww w.  j  a  va  2 s.  c  om
 * @return the value of this element.
 */
public static String getChildStringValueForElement(final Element base, final String name) {
    String value = null;
    NodeList nodeList = base.getChildNodes();
    if (nodeList.getLength() > 0) {
        int length = nodeList.getLength();
        for (int i = 0; i < length; i++) {
            Node node = nodeList.item(i);

            // Get an element, create an instance of the element
            if (Node.ELEMENT_NODE == node.getNodeType()) {
                if (name.equals(node.getNodeName())) {
                    // Get value of this child
                    Node elNode = ((Element) node).getFirstChild();
                    if (elNode != null) {
                        value = elNode.getNodeValue();
                        break;
                    }
                }
            }
        }
    }
    return value;
}

From source file:Main.java

private static String docNodeToXMLString(Node root) {
    StringBuilder result = new StringBuilder();
    if (root.getNodeType() == 3) { // TEXT_NODE
        result.append(root.getNodeValue());
    } else {//from ww w  . j  a  va 2  s  . co m
        if (root.getNodeType() != 9) { // not DOCUMENT_NODE
            StringBuffer attrs = new StringBuffer();
            for (int k = 0; k < root.getAttributes().getLength(); ++k) {
                attrs.append(" ").append(root.getAttributes().item(k).getNodeName()).append("=\"")
                        .append(root.getAttributes().item(k).getNodeValue()).append("\" ");
            }
            result.append("<").append(root.getNodeName()).append(" ").append(attrs).append(">");
        } else {
            result.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        }

        NodeList nodes = root.getChildNodes();
        for (int i = 0, j = nodes.getLength(); i < j; i++) {
            Node node = nodes.item(i);
            result.append(docNodeToXMLString(node));
        }

        if (root.getNodeType() != 9) { // not DOCUMENT_NODE
            result.append("</").append(root.getNodeName()).append(">");
        }
    }
    return result.toString();
}

From source file:Main.java

public static String getXpathExpressionValue(Object xprContext, String xpExpression)
        throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();

    XPathExpression xpe = xpath.compile(xpExpression);
    Node valueNode = (Node) xpe.evaluate(xprContext, XPathConstants.NODE);
    String value = null;// w  ww .j a v  a 2 s  .c om
    if (valueNode != null)
        value = valueNode.getNodeValue();
    if (value != null) {
        // if the node is a text node - then we trim and (potentially) look for CDATA
        if (valueNode.getNodeType() == Node.TEXT_NODE) {
            value = value.trim();

            // look for CDATA if we got nothing (stupid whitespace)
            if (value.length() == 0) {
                Node siblingForCDATA = valueNode.getNextSibling();
                if (siblingForCDATA.getNodeType() == Node.CDATA_SECTION_NODE) {
                    value = siblingForCDATA.getNodeValue();
                }
            }
        }
    }

    return value;
}

From source file:Main.java

public static List<String> getValuesFromDocumentByTagAndAttribute(Node parentNode, String tagName,
        String attributeName, String attributeValue) {
    ArrayList<String> values = new ArrayList<String>();

    for (int i = 0; i < parentNode.getChildNodes().getLength(); i++) {
        Node childNode = parentNode.getChildNodes().item(i);

        if (childNode != null) {
            if (childNode.hasAttributes()) {
                for (int j = 0; j < childNode.getAttributes().getLength(); j++) {
                    Node attribute = childNode.getAttributes().item(j);
                    if (attribute.getNodeName().equals(attributeName)
                            && attribute.getNodeValue().equals(attributeValue)) {
                        values.add(childNode.getTextContent().trim());
                    }//from  w  w  w  . j  a  va  2  s  . c  om
                }
            }

            if (childNode.hasChildNodes()) {
                values.addAll(getValuesFromDocumentByTagAndAttribute(childNode, tagName, attributeName,
                        attributeValue));
            }
        }
    }

    return values;
}