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:jp.go.nict.langrid.client.soap.io.SoapResponseParser.java

private static Node findFirstDescendantHasAttr(Node parent, String attrName, String attrValue) {
    if (parent == null)
        return null;
    Node n = parent.getFirstChild();
    while (n != null) {
        NamedNodeMap attrs = n.getAttributes();
        if (attrs != null) {
            Node attr = attrs.getNamedItem(attrName);
            if (attr != null) {
                if (attr.getNodeValue().equals(attrValue)) {
                    return n;
                }//from   ww w.j  ava 2  s  .c o m
            }
        }
        Node d = findFirstDescendantHasAttr(n, attrName, attrValue);
        if (d != null) {
            return d;
        }
        n = n.getNextSibling();
    }
    return n;
}

From source file:Main.java

/**
 * @param node// w ww .j  a v a 2s  . c om
 * @throws IOException
 */
public static void serializeNode(Node node) throws IOException {
    if (writer == null)
        writer = new BufferedWriter(new OutputStreamWriter(System.out));

    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        Document doc = (Document) node;
        writer.write("<?xml version=\"");
        writer.write(doc.getXmlVersion());
        writer.write("\" encoding=\"UTF-8\" standalone=\"");
        if (doc.getXmlStandalone())
            writer.write("yes");
        else
            writer.write("no");
        writer.write("\"?>\n");

        NodeList nodes = node.getChildNodes();
        if (nodes != null)
            for (int i = 0; i < nodes.getLength(); i++)
                serializeNode(nodes.item(i));
        break;
    case Node.ELEMENT_NODE:
        String name = node.getNodeName();
        writer.write("<" + name);
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node current = attributes.item(i);
            writer.write(" " + current.getNodeName() + "=\"");
            print(current.getNodeValue());
            writer.write("\"");
        }
        writer.write(">");

        NodeList children = node.getChildNodes();
        if (children != null) {
            //if ((children.item(0) != null) && (children.item(0).getNodeType() == Node.ELEMENT_NODE))
            //  writer.write("\n");

            for (int i = 0; i < children.getLength(); i++)
                serializeNode(children.item(i));
            if ((children.item(0) != null)
                    && (children.item(children.getLength() - 1).getNodeType() == Node.ELEMENT_NODE))
                writer.write("");
        }

        writer.write("</" + name + ">");
        break;
    case Node.TEXT_NODE:
        print(node.getNodeValue());
        break;
    case Node.CDATA_SECTION_NODE:
        writer.write("CDATA");
        print(node.getNodeValue());
        writer.write("");
        break;
    case Node.COMMENT_NODE:
        writer.write("<!-- " + node.getNodeValue() + " -->\n");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        writer.write("<?" + node.getNodeName() + " " + node.getNodeValue() + "?>\n");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writer.write("&" + node.getNodeName() + ";");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        DocumentType docType = (DocumentType) node;
        String publicId = docType.getPublicId();
        String systemId = docType.getSystemId();
        String internalSubset = docType.getInternalSubset();
        writer.write("<!DOCTYPE " + docType.getName());
        if (publicId != null)
            writer.write(" PUBLIC \"" + publicId + "\" ");
        else
            writer.write(" SYSTEM ");
        writer.write("\"" + systemId + "\"");
        if (internalSubset != null)
            writer.write(" [" + internalSubset + "]");
        writer.write(">\n");
        break;
    }
    writer.flush();
}

From source file:Main.java

/**
 For testing purpose, it print out Node list
        //from   w  w w.  j  a v a 2 s  .c  om
 @param     rows    a Nodelist
 */
public static void printNodeTypes(NodeList rows) {
    System.out.println("\tenumerating NodeList (of Elements):");
    System.out.println("\tClass\tNT\tNV");
    //iterate a given Node list
    for (int ri = 0; ri < rows.getLength(); ri++) {
        Node n = (Node) rows.item(ri);
        if (n instanceof Element) {
            System.out.print("\tElement");
        } else
            System.out.print("\tNode");

        //print out Node type and Node value
        System.out.println("\t" + n.getNodeType() + "\t" + n.getNodeValue());
    }
    System.out.println();
}

From source file:DomUtil.java

public static String getAttribute(Node element, String attName) {
    NamedNodeMap attrs = element.getAttributes();
    if (attrs == null)
        return null;
    Node attN = attrs.getNamedItem(attName);
    if (attN == null)
        return null;
    return attN.getNodeValue();
}

From source file:Main.java

/**
 * @param parentNode/*from ww  w  .  j  a  v  a  2s . c om*/
 *            is the element tag that contains all the AttirbuteValuePair
 *            tags as children
 * @return Map Returns the AV pairs in a Map where each entry in the Map is
 *         an AV pair. The key is the attribute name and the value is a Set
 *         of String objects.
 */
public static Map parseAttributeValuePairTags(Node parentNode) {

    NodeList avList = parentNode.getChildNodes();
    Map map = null;
    final int numAVPairs = avList.getLength();
    if (numAVPairs <= 0) {
        return EMPTY_MAP;
    }
    for (int l = 0; l < numAVPairs; l++) {
        Node avPair = avList.item(l);
        if ((avPair.getNodeType() != Node.ELEMENT_NODE) || !avPair.getNodeName().equals("AttributeValuePair")) {
            continue;
        }
        NodeList leafNodeList = avPair.getChildNodes();
        long numLeafNodes = leafNodeList.getLength();
        if (numLeafNodes < 2) {
            // TODO: More error handling required later for missing
            // 'Attribute' or
            // 'Value' tags.
            continue;
        }
        String key = null;
        Set values = null;
        // Since Attribute tag is always the first leaf node as per the
        // DTD,and
        // values can one or more, Attribute tag can be parsed first and
        // then
        // iterate over the values, if any.
        Node attributeNode = null;
        for (int i = 0; i < numLeafNodes; i++) {
            attributeNode = leafNodeList.item(i);
            if ((attributeNode.getNodeType() == Node.ELEMENT_NODE)
                    && (attributeNode.getNodeName().equals("Attribute"))) {
                i = (int) numLeafNodes;
            } else {
                continue;
            }
        }
        key = ((Element) attributeNode).getAttribute("name");
        // Now parse the Value tags. If there are not 'Value' tags, ignore
        // this key
        // TODO: More error handling required later for zero 'Value' tags.
        for (int m = 0; m < numLeafNodes; m++) {
            Node valueNode = leafNodeList.item(m);
            if ((valueNode.getNodeType() != Node.ELEMENT_NODE) || !valueNode.getNodeName().equals("Value")) {
                // TODO: Error handling required here
                continue;
            }
            if (values == null) {
                values = new HashSet();
            }
            Node fchild = (Text) valueNode.getFirstChild();
            if (fchild != null) {
                String value = fchild.getNodeValue();
                if (value != null) {
                    values.add(value.trim());
                }
            }
        }
        if (values == null) {
            // No 'Value' tags found. So ignore this key.
            // TODO: More error handling required later for zero
            // 'Value'tags.
            continue;
        }
        if (map == null) {
            map = new HashMap();
        }
        Set oldValues = (Set) map.get(key);
        if (oldValues != null)
            values.addAll(oldValues);
        map.put(key, values);

        // now reset values to prepare for the next AV pair.
        values = null;
    }
    if (map == null) {
        return EMPTY_MAP;
    } else {
        return map;
    }

}

From source file:de.decidr.model.workflowmodel.deployment.PackageBuilder.java

/**
 * Replaces all location attributes that match "unknow" with the new
 * endpoint//  w ww  . j a va 2s . c  om
 * 
 * @param service
 *            parsed wsdl
 * @param newEndpoint
 *            the new endpoint url
 */
private static void modifyServiceEndpoint(TDefinitions service, String newEndpoint) {
    List<TDocumented> topLevelList = service.getAnyTopLevelOptionalElement();
    List<TPort> portList = null;
    for (TDocumented node : topLevelList) {
        if (node instanceof TService) {
            TService serviceEntry = (TService) node;
            portList = serviceEntry.getPort();
        }
    }
    for (TPort port : portList) {
        for (Object portDOM : port.getAny()) {
            if (portDOM instanceof Node) {
                Node location = ((Node) portDOM).getAttributes().getNamedItem("location");
                if (location.getNodeValue().startsWith("unknown")) {
                    location.setNodeValue(newEndpoint);
                }
            }
        }
    }
}

From source file:Main.java

public static String getTextFromNode(Node node, String defaultValue) {
    if (node == null) {
        return defaultValue;
    }//w  w  w  .j ava2s. c o  m
    if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        return ((Attr) node).getValue();
    } else if (node.getNodeType() == Node.TEXT_NODE) {
        return node.getNodeValue();
    } else {
        StringBuffer text = new StringBuffer();
        getTextFromNode(node, text, true);
        return text.toString().trim();
    }
}

From source file:Main.java

/**
 * @param parentNode/*from   w  w w. jav  a2s.  c  om*/
 *            is the element tag that contains all the AttirbuteValuePair
 *            tags as children
 * @return Map Returns the AV pairs in a Map where each entry in the Map is
 *         an AV pair. The key is the attribute name and the value is a Set
 *         of String objects.
 */
public static Map<String, Set<String>> parseAttributeValuePairTags(Node parentNode) {
    NodeList keyValueList = parentNode.getChildNodes();
    int keyValueSize = keyValueList.getLength();

    if (keyValueSize <= 0) {
        return EMPTY_MAP;
    }

    Map<String, Set<String>> resultMap = null;

    for (int l = 0; l < keyValueSize; l++) {
        Node keyValueNode = keyValueList.item(l);

        if (keyValueNode.getNodeType() != Node.ELEMENT_NODE
                || !keyValueNode.getNodeName().equals("AttributeValuePair")) {
            continue;
        }

        NodeList keyValueEntryList = keyValueNode.getChildNodes();
        int keyValueEntrySize = keyValueEntryList.getLength();
        if (keyValueEntrySize < 2) {
            // TODO: More error handling required later for missing
            // 'Attribute' or 'Value' tags.
            continue;
        }

        Node keyNode = null;

        // Since Attribute tag is always the first leaf node as per the DTD, and values can one or more,
        // Attribute tag can be parsed first and then iterate over the values, if any.
        for (int i = 0; i < keyValueEntrySize; i++) {
            keyNode = keyValueEntryList.item(i);

            if (keyNode.getNodeType() == Node.ELEMENT_NODE && keyNode.getNodeName().equals("Attribute")) {
                break;
            }
        }

        final String key = ((Element) keyNode).getAttribute("name");
        Set<String> values = null;

        // Now parse the Value tags. If there are not 'Value' tags, ignore this key
        // TODO: More error handling required later for zero 'Value' tags.
        for (int m = 0; m < keyValueEntrySize; m++) {
            Node valueNode = keyValueEntryList.item(m);

            if (valueNode.getNodeType() != Node.ELEMENT_NODE || !valueNode.getNodeName().equals("Value")) {
                // TODO: Error handling required here
                continue;
            }

            if (values == null) {
                values = new HashSet<String>();
            }

            Node firstChild = valueNode.getFirstChild();
            if (firstChild != null) {
                String value = firstChild.getNodeValue();

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

        if (values == null) {
            // No 'Value' tags found. So ignore this key.
            // TODO: More error handling required later for zero
            // 'Value' tags.
            continue;
        }

        if (resultMap == null) {
            resultMap = new HashMap<String, Set<String>>();
        }

        Set<String> oldValues = resultMap.get(key);
        if (oldValues != null) {
            values.addAll(oldValues);
        }
        resultMap.put(key, values);
    }

    return resultMap == null ? EMPTY_MAP : resultMap;
}

From source file:com.weibo.wesync.notify.xml.DomUtils.java

/**
 * Extract the text value from the given DOM element, ignoring XML comments. <p>Appends all CharacterData nodes and
 * EntityReference nodes into a single String value, excluding Comment nodes.
 *
 * @see CharacterData//from   w  w w. j a  v a2  s  .  com
 * @see EntityReference
 * @see Comment
 */
public static String getTextValue(Element valueEle) {
    Assert.notNull(valueEle, "Element must not be null");
    StringBuilder sb = new StringBuilder();
    NodeList nl = valueEle.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node item = nl.item(i);
        if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
            sb.append(item.getNodeValue());
        }
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Gets the text value of the given element.
 * //  w ww  .j  a  va 2 s . co m
 * @param e the element.
 * @return the text contents of the given element.s
 */
public static String getText(Element e) {
    StringBuilder sb = null;

    if (e != null) {
        NodeList children = e.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node node = children.item(i);

            switch (node.getNodeType()) {
            case Node.TEXT_NODE:
            case Node.CDATA_SECTION_NODE:
                if (sb == null) {
                    sb = new StringBuilder();
                }
                sb.append(node.getNodeValue());
                break;
            }
        }
    }

    return (sb != null) ? sb.toString() : null;
}