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 final String getCDATA(Element elem, boolean trim) {
    StringBuffer sb = new StringBuffer();
    NodeList nl = elem.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nc = nl.item(i);
        if (nc.getNodeType() == Node.CDATA_SECTION_NODE) {
            sb.append(((Text) nc).getData());
        } else if (nc.getNodeType() == Node.TEXT_NODE) {
            String txt = ((Text) nc).getData();
            if (trim) {
                txt = txt.trim();//w ww .j ava2s. c  o  m
            }
            sb.append(txt);
        }
    }
    return sb.toString();
}

From source file:Utils.java

/**
 * <p>Returns the first child element with the given name. Returns
 * <code>null</code> if not found.</p>
 *
 * @param parent parent element/* w  w  w .ja  v a 2  s .c  om*/
 * @param name name of the child element
 * @return child element
 */
public static Element getChildElementByName(Element parent, String name) {
    NodeList children = parent.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            if (element.getTagName().equals(name)) {
                return element;
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * Trys to find a child element in the given parent element.
 *
 * @param parent The Element to search in.
 * @param name   The name of the element to search for.
 *
 * @return The Element if found, null otherwise.
 *//*www  .j  a  va  2s  .c  o m*/
public static Element findElement(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.getNodeName().equals(name)) {
                return e;
            }
        }
    }
    return null;
}

From source file:Main.java

public static boolean isElement(Node e) {
    return e.getNodeType() == Node.ELEMENT_NODE;
}

From source file:Main.java

/**
 * Gets the first element of a Node//from   www .j a  v a2  s .  c o  m
 * @param parent Node
 * @return first element of a Node
 */
public static Element getFirstElement(Node parent) {
    Node n = parent.getFirstChild();
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE) {
        n = n.getNextSibling();
    }
    if (n == null) {
        return null;
    }
    return (Element) n;
}

From source file:Main.java

public static List getChildElements(Element parent, String childName) {
    NodeList children = parent.getChildNodes();
    List list = new ArrayList();
    int size = children.getLength();

    for (int i = 0; i < size; i++) {
        Node node = children.item(i);

        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;

            if (childName.equals(element.getNodeName())) {
                list.add(element);/*w  w w .  j  ava2s.c  om*/
            }
        }
    }

    return list;
}

From source file:Main.java

public static NodeList getChildsByTagName(Element root, String name) {
    final Vector<Node> v = new Vector<Node>();
    NodeList nl = root.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Element.ELEMENT_NODE) {
            Element e = (Element) n;
            if (name.equals("*") || e.getNodeName().equalsIgnoreCase(name))
                v.add(n);/*  w w  w .jav  a2s  . c o m*/
        }
    }

    return new NodeList() {
        public Node item(int index) {
            if (index >= v.size() || index < 0)
                return null;
            else
                return v.get(index);
        }

        public int getLength() {
            return v.size();
        }
    };
}

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.
 * // w ww.j  a v  a 2  s .c  om
 * @param node
 *            The node to search from.
 * @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

/**
 * Get textual content of a node(for text nodes only).
 * //from w w w  . j ava2s.  c  o  m
 * @param node a text node.
 * @return node's textual content.
 */
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.getTextContent();
    }
    return "";
}

From source file:Main.java

public static void setPrefixRecursive(final Node node, final String prefix) {

    if (node.getNodeType() == Node.ELEMENT_NODE) {
        node.setPrefix(prefix);// w w  w.j  a  v a2  s .  c o m
    }

    final NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        setPrefixRecursive(list.item(i), prefix);
    }
}