Example usage for org.w3c.dom Node getNodeType

List of usage examples for org.w3c.dom Node getNodeType

Introduction

In this page you can find the example usage for org.w3c.dom Node getNodeType.

Prototype

public short getNodeType();

Source Link

Document

A code representing the type of the underlying object, as defined above.

Usage

From source file:Main.java

public static void cleanWhiteSpaceNodes(Element node, boolean deep) {
    NodeList list = node.getChildNodes();
    ArrayList temp = new ArrayList();
    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);
        short type = n.getNodeType();
        if (type == 1) {
            Element e = (Element) n;
            cleanWhiteSpaceNodes(e, deep);
        } else if (type == 3) {
            Text text = (Text) n;
            String val = text.getData();
            if (val.trim().equals(""))
                temp.add(text);/*from   w  w  w  .j a  va2  s. c om*/
        }
    }

    for (Iterator i = temp.iterator(); i.hasNext(); node.removeChild((Node) i.next()))
        ;
}

From source file:Main.java

public static Map<String, String> getChildTextMap(Element elt) {
    Map<String, String> data = new HashMap<String, String>();
    NodeList nodes = elt.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node child = nodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element e = (Element) child;
            data.put(e.getNodeName(), getInnerText(e));
        }// w w  w  .  j a v a  2s  .c  o m
    }
    return data;
}

From source file:Util.java

public static String getTextValue(Node node) {
    StringBuffer textValue = new StringBuffer();
    int length = node.getChildNodes().getLength();
    for (int i = 0; i < length; i++) {
        Node c = node.getChildNodes().item(i);
        if (c.getNodeType() == Node.TEXT_NODE) {
            textValue.append(c.getNodeValue());
        }//w w w .  j a va 2  s .c om
    }
    return textValue.toString().trim();
}

From source file:Main.java

/**
 * Trys to find a child element in the given parent element where the child's
 * name attribute is the given value./*from   w w w  .j a v a 2s .  c o m*/
 *
 * @param parent The Element to search in.
 * @param name   The name attribute of the element to search for.
 *
 * @return The Element if found, null otherwise.
 */
public static Element findElementWithNameAttribute(Element parent, String name) {
    NodeList l = parent.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);
        if (n.getNodeType() == n.ELEMENT_NODE) {
            Element e = (Element) n;
            if (e.getAttribute("name").equals(name)) {
                return e;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * @param node/*from w  ww  .j ava2s. c  om*/
 */
public static void displayNodeInfo(Node node) {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        System.out.println("Document Node ");
        break;
    case Node.ELEMENT_NODE:
        System.out.println("Element Node: " + node.getNodeName());
        break;
    case Node.TEXT_NODE:
        System.out.println("Text Node: " + node.getNodeName());
        break;
    case Node.CDATA_SECTION_NODE:
        System.out.println("CDATA Section Node: ");
        break;
    case Node.COMMENT_NODE:
        System.out.println("Comment Node ");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println("Processing Instruction Node ");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.println("Entity Reference Node ");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        System.out.println("Document Type Node ");
        break;
    }
}

From source file:Main.java

public static List<String> getPropertiesFromXML(Node propNode) {
    ArrayList<String> properties;
    properties = new ArrayList<String>();
    NodeList childList = propNode.getChildNodes();

    for (int i = 0; i < childList.getLength(); i++) {
        Node currentNode = childList.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            String nodeName = currentNode.getLocalName();
            String namespace = currentNode.getNamespaceURI();
            // href is a live property which is handled differently
            properties.add(namespace + ":" + nodeName);
        }/*w  w w .  j  a v a  2s.co  m*/
    }
    return properties;
}

From source file:Main.java

/**
 * Returns a List of all descendant Element nodes having the specified
 * [namespace name] property. The elements are listed in document order.
 *
 * @param node The node to search from./* w ww .j  ava  2 s  .  c o m*/
 * @param namespaceURI An absolute URI denoting a namespace name.
 * @return A List containing elements in the specified namespace; the list
 * is empty if there are no elements in the namespace.
 */
public static List<Element> getElementsByNamespaceURI(Node node, String namespaceURI) {
    List<Element> list = new ArrayList<Element>();
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        if (child.getNamespaceURI().equals(namespaceURI)) {
            list.add((Element) child);
        }
    }
    return list;
}

From source file:Main.java

public static Element getNextSibling(Element e) {
    Node n = e.getNextSibling();
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
        n = n.getNextSibling();/*from   w  ww  . j a v  a2  s. c om*/
    return (Element) n;
}

From source file:Main.java

/**
 * Get the value of an element. If the node is a Document, use the
 * document element as the element node.
 * The value of an element node is the sum of all the element's
 * first generation child text nodes. Note that this is not what you
 * would get from a mixed element in an XSL program.
 * @param node the node./*from   w ww .  j av a 2s. c  o m*/
 * @return the value of the element, or an empty string if the
 * node is not an element.
 */
public static String getElementValue(Node node) {
    if (node instanceof Document)
        node = ((Document) node).getDocumentElement();
    if (!(node instanceof Element))
        return "";
    NodeList nodeList = node.getChildNodes();
    String value = "";
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node n = nodeList.item(i);
        if (n.getNodeType() == Node.TEXT_NODE)
            value += n.getNodeValue();
    }
    return value;
}

From source file:Main.java

/**
 * Convenience method for setting a node's (attr or elem) text value.
 * /*from   ww w .j av a2  s . c  o  m*/
 * @param node     The DOM node whose value needs to be changed.
 * @param newValue The value to set for node.
 */
public static void setNodeValue(final Node node, final String newValue) {
    short nodeType = node.getNodeType();
    if (nodeType == Node.ATTRIBUTE_NODE) {
        node.setNodeValue(newValue);
    } else if (nodeType == Node.ELEMENT_NODE) {
        node.setTextContent(newValue);
    }
}