Example usage for org.w3c.dom Node getNextSibling

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

Introduction

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

Prototype

public Node getNextSibling();

Source Link

Document

The node immediately following this node.

Usage

From source file:Main.java

public static <T> Map<String, T> loadBeans(Node parent, Class<T> beanClass)
        throws IntrospectionException, InstantiationException, IllegalAccessException, IllegalArgumentException,
        DOMException, InvocationTargetException {
    Map<String, T> store = new HashMap<String, T>();

    Node node = parent.getFirstChild();
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            String id = getId(node);
            T bean = loadBean(node, beanClass);
            store.put(id, bean);/*from w w w  .j ava2s  .  c o m*/
        }
        node = node.getNextSibling();
    }

    return store;
}

From source file:Main.java

public static Element childNodeByTag(Node node, String childNodeName) {
    if (node == null)
        return null;

    Element childElement = null;/*from   w  w w  . j a va 2 s .  c o m*/
    Node childNode = node.getFirstChild();
    if (childNode != null) {
        do {
            if (childNode.getNodeType() == Node.ELEMENT_NODE
                    && (childNodeName == null || childNodeName.equals(childNode.getNodeName()))) {
                return (Element) childNode;
            }
        } while ((childNode = childNode.getNextSibling()) != null);
    }

    return null;
}

From source file:Main.java

private static void collectResults(Element element, String[] path, int index, Collection destination) {
    // If we matched all the way to the leaf of the path, add the element to the destination....
    String elemName = element.getNodeName();
    int lastColon = elemName.lastIndexOf(':');
    if (lastColon > 0) {
        elemName = elemName.substring(0, lastColon);
    }//from  www  . ja  v a2  s.  co m
    if (!elemName.equals(path[index]))
        return; // No match in this subtree

    if (index >= path.length - 1) {
        destination.add(element);
        return;
    }

    // OK, we have a match on the path so far, now check rest of the path (possibly none)
    Node child = element.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            // Recursive step, try t
            collectResults((Element) child, path, index + 1, destination);
        }
        child = child.getNextSibling();
    }
}

From source file:Main.java

/**
 * @param sibling/*ww  w .  jav a2  s  .  c  o  m*/
 * @param uri
 * @param nodeName
 * @param number
 * @return nodes with the constrain
 */
public static Element selectNode(Node sibling, String uri, String nodeName, int number) {
    while (sibling != null) {
        if (nodeName.equals(sibling.getLocalName()) && uri.equals(sibling.getNamespaceURI())) {
            if (number == 0) {
                return (Element) sibling;
            }
            number--;
        }
        sibling = sibling.getNextSibling();
    }
    return null;
}

From source file:Main.java

/**
 * Get all the direct children elements of an element that have a specific
 * tag name./*from   w  w w.  j  a  va 2  s. c o m*/
 *
 * @param parent
 *            The parent element.
 * @param name
 *            The tag name to match.
 *
 * @return A list of Element's.
 */
public static List<Element> getElements(final Element parent, final String name) {
    final LinkedList<Element> list = new LinkedList<Element>();

    Node node = parent.getFirstChild();

    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            final Element element = (Element) node;

            if (element.getTagName().equals(name)) {
                list.add(element);
            }
        }

        node = node.getNextSibling();
    }

    return list;
}

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
 *//*  w ww.  jav  a2s.c o m*/
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);
}

From source file:Main.java

public static List childNodeList(Node node, String childNodeName) {
    if (node == null)
        return null;

    List children = new LinkedList();
    Node childNode = node.getFirstChild();
    if (childNode != null) {
        do {/*from w  w  w .  j a v a 2  s.c o m*/
            if (childNode.getNodeType() == Node.ELEMENT_NODE
                    && (childNodeName == null || childNodeName.equals(childNode.getNodeName()))) {
                children.add(childNode);
            }
        } while ((childNode = childNode.getNextSibling()) != null);
    }

    return children;
}

From source file:Main.java

/**
 * @param sibling/*from  ww w .  j  ava  2s  .co  m*/
 * @param uri
 * @param nodeName
 * @return nodes with the constrain
 */
public static Element[] selectNodes(Node sibling, String uri, String nodeName) {
    List<Element> list = new ArrayList<Element>();
    while (sibling != null) {
        if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri)
                && sibling.getLocalName().equals(nodeName)) {
            list.add((Element) sibling);
        }
        sibling = sibling.getNextSibling();
    }
    return list.toArray(new Element[list.size()]);
}

From source file:ua.kiev.doctorvera.utils.SMSGateway.java

public static final String getElementValue(Node elem) {
    Node child;
    if (elem != null) {
        if (elem.hasChildNodes()) {
            for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
                if (child.getNodeType() == Node.TEXT_NODE) {
                    return child.getNodeValue();
                }// www  .j  a va 2 s .  c om
            }
        }
    }
    return "";
}

From source file:Main.java

/**
 * Returns the next sibling Element for an element, optionally matching tag names.
 * //from  w ww.  ja v a 2  s  . co m
 * @param child the element from which to search for a next sibling
 * @param sameName true to retrive the next sibling element of the same name, of false if any name is allowed
 * @return the next sibling element if one exists, or null otherwise
 */
static public Element getNextSiblingElement(Element child, boolean sameName) { // Child Element suchen
    if (child == null)
        return null;
    String name = child.getTagName();
    Node node = child.getNextSibling();
    while (node != null) { // Find all Element nodes
        if (node.getNodeType() == Node.ELEMENT_NODE) { // check name
            Element elem = (Element) node;
            if (sameName && name.equalsIgnoreCase(elem.getTagName()))
                return elem; // found
        }
        node = node.getNextSibling();
    }
    return null; // not found!
}