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

static public List<Element> getChildElementList(Element root) {
    List<Element> list = new ArrayList<>();
    NodeList nodes = root.getChildNodes();
    int i, size = nodes.getLength();
    Node node;

    for (i = 0; i < size; i++) {
        node = nodes.item(i);//from   w ww  .j  ava 2 s .  c  om

        if (node.getNodeType() == Node.ELEMENT_NODE)
            list.add((Element) node);
    }

    return list;
}

From source file:Main.java

/**
 * Find element.//from www . j  a va  2 s .  c om
 * 
 * @param topElm
 *        the top elm
 * @param localName
 *        the local name
 * @param namespace
 *        the namespace
 * @return the element
 */
public static Element findElement(Element topElm, String localName, String namespace) {
    Stack<Element> stack = new Stack<Element>();
    stack.push(topElm);
    while (!stack.isEmpty()) {
        Element curElm = stack.pop();
        if ((curElm.getLocalName().equals(localName))
                && (namespacesAreSame(curElm.getNamespaceURI(), namespace))) {
            return curElm;
        }
        NodeList childNodes = curElm.getChildNodes();
        for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
            Node item = childNodes.item(i);
            if (item.getNodeType() == Node.ELEMENT_NODE) {
                stack.push((Element) item);
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Returns the path expression for a given node.
 * Path expressions look like: Foo.Bar.Poo where elements are
 * separated with a dot character.//  www .j a v  a  2 s. c  o  m
 *
 * @param node in DOM tree.
 * @return the path expression representing the node in DOM tree.
 */
public static String getNodesPathName(Node node) {
    final StringBuffer buffer = new StringBuffer();

    if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        buildNodeName(((Attr) node).getOwnerElement(), buffer);
        buffer.append(".");
        buffer.append(node.getLocalName());
    } else {
        buildNodeName(node, buffer);
    }

    return buffer.toString();
}

From source file:DOMDump.java

private static void dumpLoop(Node node, String indent) {
    switch (node.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
        System.out.println(indent + "CDATA_SECTION_NODE");
        break;/*from  w  w  w .  java2s.  c o m*/
    case Node.COMMENT_NODE:
        System.out.println(indent + "COMMENT_NODE");
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
        break;
    case Node.DOCUMENT_NODE:
        System.out.println(indent + "DOCUMENT_NODE");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        System.out.println(indent + "DOCUMENT_TYPE_NODE");
        break;
    case Node.ELEMENT_NODE:
        System.out.println(indent + "ELEMENT_NODE");
        break;
    case Node.ENTITY_NODE:
        System.out.println(indent + "ENTITY_NODE");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.println(indent + "ENTITY_REFERENCE_NODE");
        break;
    case Node.NOTATION_NODE:
        System.out.println(indent + "NOTATION_NODE");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
        break;
    case Node.TEXT_NODE:
        System.out.print(indent + "TEXT_NODE");
        System.out.println(" : " + node.getTextContent());
        break;
    default:
        System.out.println(indent + "Unknown node");
        break;
    }

    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        dumpLoop(list.item(i), indent + "   ");
    }
}

From source file:Main.java

static public Element findChildElement(Node first, Node last, String name) {
    while (first != last) {
        if (first.getNodeType() == Node.ELEMENT_NODE) {
            if (first.getNodeName().equals(name))
                return (Element) first;
        }//from w  w  w  .  j  a  v  a2  s . c o m
        first = first.getNextSibling();
    }
    return null;
}

From source file:Main.java

public static int getCurrentPosition(Node refNode) {
    if (refNode == null) {
        return -1;
    }//  w w w  . j a v  a  2  s .  c  o  m

    int counter = 0;
    Node current = refNode;

    while (current != null) {
        if (current.getNodeType() == Node.ELEMENT_NODE) {
            counter++;
        }

        current = current.getPreviousSibling();
    }

    return counter;
}

From source file:Main.java

public static Element getChildElement(Element parent, String childName) {
    NodeList list = parent.getElementsByTagName(childName);

    Node node;
    for (int i = 0; i < list.getLength(); i++) {
        node = list.item(i);/*ww w .  j  a  v  a2  s  . co  m*/
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) node;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Get all child elements of the specified (root) element, that are named by the specified name. If the root is null,
 * an illegal argument exception is thrown. If the root has no children by the specified name, an empty array is
 * returned.//from ww w. j a v a 2 s  .co  m
 * 
 * @param root The element to search.
 * @param name The name of the child elements to look for.
 * @return An array of the child elements named by the specified name, of the root.
 */
public static ArrayList<Element> getElements(Element root, String name) {
    if (root == null || name == null || name.length() <= 0)
        throw new IllegalArgumentException("Null or invalid root element or name!");
    NodeList lst = root.getChildNodes();
    int size = lst.getLength();
    ArrayList<Element> a = new ArrayList<Element>(size / 2);
    name = localName(name);
    for (int i = 0; i < size; i++) {
        Node node = lst.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            String nodeName = localName(node);
            if (name.equals(nodeName))
                a.add((Element) node);
        }
    }
    return a;
}

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  . ja  v  a 2 s.  c  om*/
        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 getNodeHierarchy(Node node) {

    StringBuffer sb = new StringBuffer();
    if (node != null) {
        Stack<Node> st = new Stack<Node>();
        st.push(node);/*from ww w  . j a va  2 s  .  c o  m*/

        Node parent = node.getParentNode();

        while ((parent != null) && (parent.getNodeType() != Node.DOCUMENT_NODE)) {

            st.push(parent);

            parent = parent.getParentNode();
        }

        // constructs node hierarchy
        Node n = null;
        while (!st.isEmpty() && null != (n = st.pop())) {

            if (n instanceof Element) {
                Element e = (Element) n;

                if (sb.length() == 0) {
                    sb.append(e.getTagName());
                } else {
                    sb.append("/" + e.getTagName());
                }
            }

        }
    }
    return sb.toString();

}