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

private static String getValueByTagName(Node n, String tag) {
    if (n == null)
        return null;
    if (tag.equals(n.getLocalName()))
        return n.getFirstChild().getNodeValue();
    if (n.hasChildNodes())
        return getValueByTagName(n.getFirstChild(), tag);
    else if (n.getNextSibling() != null)
        return getValueByTagName(n.getNextSibling(), tag);
    else// ww  w  .  j  a v a 2 s . c  o m
        return getValueByTagName(n.getParentNode().getNextSibling(), tag);
}

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;
        }/*w  ww .ja v a 2s .c  om*/
        first = first.getNextSibling();
    }
    return null;
}

From source file:Main.java

public static Element findChildElementWithAttribute(Element parent, String name, String attribute,
        String value) {/*from  ww w  .  ja va 2  s .  c om*/
    if (parent == null) {
        return null;
    }
    org.w3c.dom.Node ret = parent.getFirstChild();
    while (ret != null && (!(ret instanceof Element) || !ret.getNodeName().equals(name)
            || ((Element) ret).getAttribute(attribute) == null
            || !((Element) ret).getAttribute(attribute).equals(value))) {
        ret = ret.getNextSibling();
    }
    return (Element) ret;
}

From source file:Main.java

public static boolean hasSameNamedSibling(Node node) {
    String s = node.getNodeName();
    for (Node node1 = node.getPreviousSibling(); node1 != null; node1 = node1.getPreviousSibling())
        if (node1.getNodeName().equals(s) && node1.getNodeType() == node.getNodeType())
            return true;

    for (Node node2 = node.getNextSibling(); node2 != null; node2 = node2.getNextSibling())
        if (node2.getNodeName().equals(s) && node2.getNodeType() == node.getNodeType())
            return true;

    return false;
}

From source file:Main.java

/**
 * Returns a list of elements having a given tag
 *//*from   www .j a v  a 2  s.  c o  m*/
public static List<Element> getElements(Element element, String tagName) {
    Node node = element.getFirstChild();
    List<Element> elements = new ArrayList<Element>();

    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().compareTo(tagName) == 0)
            elements.add((Element) node);

        node = node.getNextSibling();
    }

    return elements;
}

From source file:Main.java

/**
 *   Find the first child node matching the given tag name. Only searches 
 *   1st-level children; not a recursive search. Null if not found.
 *//*from w w w . j  a v a 2 s .c  o m*/
public static Node findChildNodeMatching(Node root, String tagName, short nodeType) {
    Node childNode = root.getFirstChild();
    while (childNode != null) {
        if (childNode.getNodeType() == nodeType) {
            if (tagName.equals(childNode.getNodeName())) {
                return childNode;
            }
        }

        childNode = childNode.getNextSibling();
    }

    return null;
}

From source file:Main.java

/**
 * This method returns a list of the direct element node children of this element node with the specified tag.
 * @param node - parent node/*from   w ww. j a v a2s.  c o  m*/
 * @param tag - tag of direct children to be returned
 * @return a list containing the direct element children with the given tag
 * @author Tristan Bepler
 */
public static List<Element> getDirectChildElementsByTag(Element node, String tag) {
    List<Element> children = new ArrayList<Element>();
    Node child = node.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE
                && (child.getNodeName().equals(tag) || tag.equals(DOM_WILDCARD))) {
            children.add((Element) child);
        }
        child = child.getNextSibling();
    }
    return children;
}

From source file:Main.java

public static Element getChildElementByLocalName(Element parentElem, String localName) {
    Node child = parentElem.getFirstChild();
    while (child != null) {
        if ((child.getNodeType() == Node.ELEMENT_NODE) && getLocalName(child).equals(localName)) {
            return (Element) child;
        }//from   ww w  .j a v  a2 s.c  o  m
        child = child.getNextSibling();
    }
    return null;
}

From source file:Main.java

private static boolean serializeXmlNode(Node node, Writer writer, boolean includeNode) throws IOException {
    if (node == null) {
        return false;
    }/*from w ww.jav a  2 s.  c om*/
    short type = node.getNodeType();
    boolean result = true;
    switch (type) {
    case Node.ATTRIBUTE_NODE: {
        String text = ((Attr) node).getValue();
        writer.write(text);
        break;
    }
    case Node.TEXT_NODE: {
        String text = ((Text) node).getData();
        writer.write(text);
        break;
    }
    case Node.ELEMENT_NODE: {
        Element element = (Element) node;
        if (includeNode) {
            serializeXML(element, writer, false);
        } else {
            Node child = element.getFirstChild();
            while (child != null) {
                serializeXmlNode(child, writer, true);
                child = child.getNextSibling();
            }
        }
        break;
    }
    case Node.DOCUMENT_NODE: {
        Document doc = (Document) node;
        serializeXmlNode(doc.getDocumentElement(), writer, includeNode);
        break;
    }
    default:
        result = false;
        break;
    }
    return result;
}

From source file:DomUtil.java

/**
 * Get the first direct child with a given type
 *//*from w  w  w  . ja v  a2  s.  c o  m*/
public static Node getChild(Node parent, int type) {
    Node n = parent.getFirstChild();
    while (n != null && type != n.getNodeType()) {
        n = n.getNextSibling();
    }
    if (n == null)
        return null;
    return n;
}