Example usage for org.w3c.dom Node getClass

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

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:Main.java

/**
 * Extracts the {@link Element} for the given name from the parent.
 * /*w w w . j av a2s.  c  om*/
 * @param parent
 * @param name
 * @return
 */
public static <T extends Node> T findNode(Node parent, Class<T> type, String name) {
    final NodeList childNodes = parent.getChildNodes();

    for (int i = 0; i < childNodes.getLength(); i++) {
        final Node child = childNodes.item(i);
        if (type.isAssignableFrom(child.getClass()) && name.equals(child.getNodeName())) {
            return type.cast(child);
        }
    }
    return null;
}

From source file:Main.java

/**
 * Get all child nodes of parent that implement/subclass the given type.
 * //from ww w. j a v  a 2 s  .  c o  m
 * @param parent
 *            Parent to search
 * @param nodeType
 *            Type of child to search for
 * @return Array of children of parent that conform to the given nodeType
 */
public static <N extends Node> N[] getChildrenImplementing(Node parent, Class<N> nodeType) {
    if (parent == null) {
        return null;
    }
    NodeList children = parent.getChildNodes();
    if (children == null) {
        return null;
    }
    int count = 0;
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (nodeType.isAssignableFrom(node.getClass())) {
            count++;
        }
    }
    @SuppressWarnings("unchecked")
    N[] array = (N[]) Array.newInstance(nodeType, count);
    for (int i = 0, pos = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (nodeType.isAssignableFrom(node.getClass())) {
            @SuppressWarnings("unchecked")
            N n = (N) node;
            Array.set(array, pos++, n);
        }
    }
    return array;
}

From source file:Main.java

/**
 * Get the index'th child node of parent that implements/subclasses the
 * given type.//from w ww  . j a  v  a2 s  .  c  o  m
 * 
 * @param index
 *            Child node index
 * @param parent
 *            Parent to search
 * @param nodeType
 *            Type of child to search for
 * @return index'th child of parent that conforms to the given nodeType
 */
public static <N extends Node> N getNthChildImplementing(int index, Node parent, Class<N> nodeType) {
    if (parent == null) {
        return null;
    }
    NodeList children = parent.getChildNodes();
    if (children == null) {
        return null;
    }
    int count = 0;
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (nodeType.isAssignableFrom(node.getClass())) {
            if (count++ == index) {
                @SuppressWarnings("unchecked")
                N n = (N) node;
                return n;
            }
        }
    }
    return null;
}

From source file:Main.java

public static List<Element> elementsQName(final Element element, final Set<QName> allowedTagNames) {
    List<Element> elements = null;
    final NodeList nodeList = element.getChildNodes();
    if (nodeList != null) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            final Node child = nodeList.item(i);
            if (Element.class.isAssignableFrom(child.getClass())) {
                final Element childElement = (Element) child;
                final QName childElementQName = new QName(childElement.getNamespaceURI(),
                        childElement.getLocalName());
                if (allowedTagNames.contains(childElementQName)) {
                    if (elements == null) {
                        elements = new ArrayList<Element>();
                    }/*from  w  w  w. j  a va  2s . c o m*/
                    elements.add(childElement);
                }
            }
        }
    }
    return elements;
}

From source file:Main.java

/**
 * Dumps a debug listing of the child nodes of the given node to
 * System.out./*  ww  w  .  ja  va 2s  .c o m*/
 * 
 * @param node the node to dump the children of
 */
public static void dumpChildren(Node node) {
    System.out.println("Children of " + node.getNodeName() + ", NS: " + node.getNamespaceURI() + ", Type: "
            + node.getClass());
    Node child = node.getFirstChild();
    while (child != null) {
        short nodeType = child.getNodeType();
        String nodeName = child.getNodeName();
        String nodeValue = child.getNodeValue();
        String nsURI = child.getNamespaceURI();
        System.out.println("  Type: " + nodeType + ", Name: " + nodeName + ", Value: " + nodeValue + ", NS: "
                + nsURI + ", Type: " + node.getClass());
        child = child.getNextSibling();
    }
}

From source file:TryDOM.java

static void listNodes(Node node, String indent) {
        String nodeName = node.getNodeName();
        System.out.println(indent + nodeName + " Node, type is " + node.getClass().getName() + ":");
        System.out.println(node);

        if (node instanceof Element && node.hasAttributes()) {
            NamedNodeMap attrs = node.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Attr attribute = (Attr) attrs.item(i);
                System.out.println(indent + attribute.getName() + "=" + attribute.getValue());
            }//from w w  w  .j  a va  2 s  .  c o m
        }
        NodeList list = node.getChildNodes();
        if (list.getLength() > 0) {
            System.out.println(indent + "Child Nodes of " + nodeName + " are:");
            for (int i = 0; i < list.getLength(); i++)
                listNodes(list.item(i), indent + " ");
        }
    }

From source file:Main.java

public static List<Element> elements(final Element element, final String tagName) {
    List<Element> elements = null;
    final NodeList nodeList = element.getChildNodes();
    if (nodeList != null) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            final Node child = nodeList.item(i);
            if (Element.class.isAssignableFrom(child.getClass())) {
                final Element childElement = (Element) child;
                final String childTagName = getTagLocalName(childElement);
                if (childTagName.equals(tagName)) {
                    if (elements == null) {
                        elements = new ArrayList<Element>();
                    }//  w  ww. j  a v a2  s  .  co  m
                    elements.add(childElement);
                }
            }
        }
    }
    return elements;
}

From source file:Main.java

public static List<Element> elements(final Element element, final Set<String> allowedTagNames) {
    List<Element> elements = null;
    final NodeList nodeList = element.getChildNodes();
    if (nodeList != null) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            final Node child = nodeList.item(i);
            if (Element.class.isAssignableFrom(child.getClass())) {
                final Element childElement = (Element) child;
                final String childTagName = getTagLocalName(childElement);
                if (allowedTagNames.contains(childTagName)) {
                    if (elements == null) {
                        elements = new ArrayList<Element>();
                    }//from w  w w . j a va2 s. c om
                    elements.add(childElement);
                }
            }
        }
    }
    return elements;
}

From source file:Main.java

public static Element element(final Element element, final String tagName) {
    Element childElement = null;/*  w  w w.j  ava 2 s .  c  o  m*/
    if (element != null) {
        final NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength() && childElement == null; i++) {
            final Node child = nodeList.item(i);
            if (Element.class.isAssignableFrom(child.getClass())
                    && getTagLocalName((Element) child).equals(tagName)) {
                childElement = (Element) child;
            }
        }
    }
    return childElement;
}

From source file:Main.java

public static String GetStringValueForNode(Node item) throws Exception {
    if (item instanceof Element) {
        StringBuilder builder = new StringBuilder();
        NodeList children = item.getChildNodes();
        for (int index = 0; index < children.getLength(); index++)
            builder.append(children.item(index).getNodeValue());

        // return...
        return builder.toString();
    } else/*from  w ww.  j a v  a  2s .com*/
        throw new Exception(String.format("Cannot handle '%s'.", item.getClass()));
}