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 Map<String, String> grabProperty(Map<String, String> map, Element elem) {
    for (Entry<String, String> entry : map.entrySet()) {
        String key = entry.getKey();
        String val = "";
        NodeList nodeList = elem.getElementsByTagName(key);
        if (nodeList == null || nodeList.getLength() <= 0) {
            //entry.setValue("");
        } else {//ww w.j  av a2  s  .  c  om
            Node node = nodeList.item(0);
            if (node != null)
                node = node.getFirstChild();
            if (node != null)
                val = node.getNodeValue();
            //String val=nodeList.item(0).getFirstChild().getNodeValue();
            //entry.setValue("");
        }
        entry.setValue(val);

    }
    return map;
}

From source file:Main.java

public static List<Node> getNodesWithKey(Node parent, String key, Set<String> values, boolean all_of_them) {
    ArrayList<Node> valid = new ArrayList<>();
    NodeList children = parent.getChildNodes();
    Node current;// w  w  w .j a v a2 s.  c  o  m
    for (int i = 0; i < children.getLength(); i++) {
        current = children.item(i);
        NamedNodeMap attrs = current.getAttributes();
        if (attrs != null) {
            Node keynode = attrs.getNamedItem(key);
            if (keynode != null)
                if (values == null || values.contains(keynode.getNodeValue())) {
                    valid.add(current);
                    if (!all_of_them)
                        break;
                }
        }
    }
    return valid;
}

From source file:Main.java

/** Takes XML node and prints to String.
 *
 * @param node Element to print to String
 * @return XML node as String/*from w  w  w  .  j  av  a2  s  .  c o m*/
 */
private static void printNode(StringBuffer sBuffer, Node node) {
    if (node.getNodeType() == Node.TEXT_NODE) {
        sBuffer.append(encodeXMLText(node.getNodeValue().trim()));
    } else if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
        sBuffer.append("<![CDATA[");
        sBuffer.append(node.getNodeValue());
        sBuffer.append("]]>");
    } else if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element el = (Element) node;

        sBuffer.append("<").append(el.getTagName());

        NamedNodeMap attribs = el.getAttributes();

        for (int i = 0; i < attribs.getLength(); i++) {
            Attr nextAtt = (Attr) attribs.item(i);
            sBuffer.append(" ").append(nextAtt.getName()).append("=\"").append(nextAtt.getValue()).append("\"");
        }

        NodeList nodes = node.getChildNodes();

        if (nodes.getLength() == 0) {
            sBuffer.append("/>");
        } else {
            sBuffer.append(">");

            for (int i = 0; i < nodes.getLength(); i++) {
                printNode(sBuffer, nodes.item(i));
            }

            sBuffer.append("</").append(el.getTagName()).append(">");
        }
    }
}

From source file:de.mpg.imeji.presentation.metadata.extractors.BasicExtractor.java

static void displayMetadata(List<String> techMd, Node node, int level) {
    StringBuffer sb = new StringBuffer();
    // print open tag of element
    indent(techMd, sb, level);/*from ww  w.  j a v  a 2  s. c om*/
    sb.append("<" + node.getNodeName());
    NamedNodeMap map = node.getAttributes();
    if (map != null) {
        // print attribute values
        int length = map.getLength();
        for (int i = 0; i < length; i++) {
            Node attr = map.item(i);
            sb.append(" " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\"");
        }
    }
    Node child = node.getFirstChild();
    if (child == null) {
        // no children, so close element and return
        sb.append("/>");
        techMd.add(sb.toString());
        sb.delete(0, sb.length());
        return;
    }
    // children, so close current tag
    sb.append(">");
    techMd.add(sb.toString());
    sb.delete(0, sb.length());
    while (child != null) {
        // print children recursively
        displayMetadata(techMd, child, level + 1);
        child = child.getNextSibling();
    }
    // print close tag of element
    indent(techMd, sb, level);
    sb.append("</" + node.getNodeName() + ">");
    techMd.add(sb.toString());
    sb.delete(0, sb.length());
}

From source file:Main.java

/**
 * Returns a Map of all attributes of the given element with the given namespace.
 * Why can we not create our own NamedNodeMaps? This would save trouble.
 * @param element the elment from which to retrieve the attributes.
 * @param namespaceURI the namespace of the attributes to retrieve.
 * @return a Map containing the attributes names and their values.
 *//*from   www .  jav a 2 s  .  c o m*/
public static Map getAttributesWithNS(Element element, String namespaceURI) {
    Map result = new HashMap();

    NamedNodeMap attributes = element.getAttributes();

    if (attributes == null)
        return result;

    for (int i = 0; i != attributes.getLength(); i++) {
        Node attribute = attributes.item(i);

        if (namespaceURI == null && attribute.getNamespaceURI() == null) {
            result.put(attribute.getNodeName(), attribute.getNodeValue());
        } else if (attribute.getNamespaceURI() != null && attribute.getNamespaceURI().equals(namespaceURI)) {
            result.put(attribute.getNodeName(), attribute.getNodeValue());
        }
    }

    return result;
}

From source file:Main.java

public static String cdataValue(Element element) {
    if (element == null)
        return null;
    // get the first element with the given name
    Node node = element.getFirstChild();

    if (node != null) {
        do {/* ww w  .j  a v  a 2  s. c  o  m*/
            if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
                return node.getNodeValue();
            }
        } while ((node = node.getNextSibling()) != null);
    }
    return null;
}

From source file:Main.java

public static ArrayList getChildTextNodeValues(NodeList theNodeList) {
    ArrayList arrayList = new ArrayList();

    for (int i = 0; i < theNodeList.getLength(); i++) {
        Node node = getTextNode(theNodeList.item(i));
        if (node != null) {
            String s = node.getNodeValue();
            arrayList.add(s);//  w  w  w .  jav a 2 s.  c o  m
        }
    }
    return arrayList;
}

From source file:Main.java

public static Object getBean(String args) {
    try {//from   w  ww  .  jav a2 s .c om
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc;
        doc = builder.parse(new File("config.xml"));
        NodeList nl = null;
        Node classNode = null;
        String cName = null;
        nl = doc.getElementsByTagName(args);

        classNode = nl.item(0).getFirstChild();
        cName = classNode.getNodeValue();

        Class c = Class.forName(cName);
        Object obj = c.newInstance();
        return obj;

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.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 ww .ja  v a2 s .c o m
 * @see EntityReference
 * @see Comment
 */
public static String getTextValue(Element valueEle) {
    if (valueEle == null)
        throw new IllegalArgumentException("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());
        } else if (item instanceof Element) {
            sb.append(getTextValue((Element) item));
        }
    }
    return sb.toString();
}

From source file:Main.java

/**
 * A method to get the value of desire node from xml document
 * @param Node parent, xml's node object to get
 * @return String , value from provided node
 *///from  ww w.j ava 2s .com
static public String getNodeValue(Node parent) {
    String ret = "";

    Node n = parent.getFirstChild();
    while (n != null) {
        if (n.getNodeType() == Node.TEXT_NODE) {
            try {
                ret = n.getNodeValue().trim();
            } catch (NullPointerException ex) {
                ret = "";
                break;
            }
        }
        n = n.getNextSibling();
    }
    return (ret);
}