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 boolean hasOnlyTextSiblings(/*@Nonnull*/Node node) {
    Node leftSibling = node.getPreviousSibling();

    while (leftSibling != null) {
        if (!(leftSibling instanceof Text)) {
            return false;
        }//from  w  ww.  ja v  a 2s.  co m

        leftSibling = leftSibling.getPreviousSibling();
    }

    Node rightSibling = node.getNextSibling();

    while (rightSibling != null) {
        if (!(rightSibling instanceof Text)) {
            return false;
        }

        rightSibling = rightSibling.getNextSibling();
    }

    return true;
}

From source file:Main.java

/**
 * @param n1 first Node to test//ww w .j av a2s  .co m
 * @param n2 second Node to test
 * @return true if a deep compare show the same children and attributes in
 * the same order
 */
public static boolean equals(Node n1, Node n2) {
    // compare type
    if (!n1.getNodeName().equals(n2.getNodeName())) {
        return false;
    }
    // compare attributes
    NamedNodeMap nnm1 = n1.getAttributes();
    NamedNodeMap nnm2 = n2.getAttributes();
    if (nnm1.getLength() != nnm2.getLength()) {
        return false;
    }
    for (int i = 0; i < nnm1.getLength(); i++) {
        Node attr1 = nnm1.item(i);
        if (!getAttribute(n1, attr1.getNodeName()).equals(getAttribute(n2, attr1.getNodeName()))) {
            return false;
        }
    }
    // compare children
    Node c1 = n1.getFirstChild();
    Node c2 = n2.getFirstChild();
    for (;;) {
        while ((c1 != null) && c1.getNodeName().startsWith("#")) {
            c1 = c1.getNextSibling();
        }
        while ((c2 != null) && c2.getNodeName().startsWith("#")) {
            c2 = c2.getNextSibling();
        }
        if ((c1 == null) && (c2 == null)) {
            break;
        }
        if ((c1 == null) || (c2 == null)) {
            return false;
        }
        if (!equals(c1, c2)) {
            return false;
        }
        c1 = c1.getNextSibling();
        c2 = c2.getNextSibling();
    }
    return true;
}

From source file:com.buzzdavidson.spork.util.XmlUtils.java

/**
 * Descends from the given parent looking for the first node to match path.
 * @param parent The DOM parent from which to start.
 * @param path The set of regular expressions for each level of the descent.
 * @return The child node at the end of the path or null if no match.
 */// w w  w  .  ja va2  s. co m
public static Node getChild(Node parent, Pattern[] path) {
    Node c = parent;
    int i;

    for (i = 0; c != null && i < path.length; ++i) {
        logger.debug("Looking for node matching [" + path[i].pattern() + "]");
        for (c = c.getFirstChild(); c != null; c = c.getNextSibling()) {
            String nn = c.getNodeName();
            Matcher m = path[i].matcher(nn);
            //logger.info("Checking node: " + nn);
            if (m != null && m.matches()) {
                logger.debug("Found matching node: " + nn);
                break;
            }
        }
    }

    return (i == path.length) ? c : null;
}

From source file:Main.java

public static boolean hasOnlyTextSiblings(@Nonnull Node node) {
    Node leftSibling = node.getPreviousSibling();

    while (leftSibling != null) {
        if (!(leftSibling instanceof Text)) {
            return false;
        }/*from  w w w . jav a  2 s . c  om*/

        leftSibling = leftSibling.getPreviousSibling();
    }

    Node rightSibling = node.getNextSibling();

    while (rightSibling != null) {
        if (!(rightSibling instanceof Text)) {
            return false;
        }

        rightSibling = rightSibling.getNextSibling();
    }

    return true;
}

From source file:Main.java

public static Double[] getDataList(Node sampleNode) {
    if (sampleNode == null)
        return new Double[0];
    List<Double> measureValues = new ArrayList<Double>();
    Node n = sampleNode.getFirstChild();
    while (n != null) {
        if (n.getNodeName().equalsIgnoreCase("data")) {
            String ms = n.getFirstChild().getNodeValue();
            double mesVal = Double.parseDouble(ms);
            measureValues.add(new Double(mesVal));
        }//from w  ww .j a  va 2 s . c  o m
        n = n.getNextSibling();
    }
    return measureValues.toArray(new Double[0]);
}

From source file:Main.java

static public Element[] findChildElements(Node first, Node last, String name) {
    Vector v = new Vector();
    while (first != last) {
        if (first.getNodeType() == Node.ELEMENT_NODE) {
            if (first.getNodeName().equals(name))
                v.addElement(first);/*from   w w  w  .  j a  v  a2  s  . c o m*/
        }
        first = first.getNextSibling();
    }
    Element array[] = new Element[v.size()];
    for (int i = 0; i < array.length; ++i) {
        array[i] = (Element) v.elementAt(i);
    }
    return array;
}

From source file:Main.java

/** Finds and returns the first child node with the given name. */
public static Element getFirstChildElement(Node parent, String elemName) {

    if (parent == null)
        return null;
    // search for node
    Node child = parent.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            if (child.getNodeName().equals(elemName)) {
                return (Element) child;
            }//ww  w  . java 2 s  . c om
        }
        child = child.getNextSibling();
    }

    // not found
    return null;

}

From source file:jp.go.nict.langrid.client.soap.io.SoapResponseParser.java

private static Node findFirstChildNamed(Node parent, String localName) {
    Node n = parent.getFirstChild();
    while (n != null && ((n.getNodeType() != Node.ELEMENT_NODE) || !n.getLocalName().equals(localName))) {
        n = n.getNextSibling();
    }//from   w ww .j a  va2  s.  co m
    return n;
}

From source file:Main.java

static public Element findChildElementNS(Node first, Node last, String namespaceuri, String qname) {
    while (first != last) {
        if (first.getNodeType() == Node.ELEMENT_NODE) {
            if (isA(first, namespaceuri, qname)) {
                return (Element) first;
            }//from   www  .  ja  va2 s.  c  o  m

        }
        first = first.getNextSibling();
    }
    return null;
}

From source file:Main.java

private final static String getPathElement(final Node n) {
    final String name = getLocalName(n);
    int i = 1;/*  www . j a  v a  2 s.c  o  m*/

    if (n instanceof Attr) {
        return "@" + name; // Still need to check namespaces; could have 2 with same local name
    }

    // Could use attributes/namespaces to distinguish between nodes with the same local name
    // instead of just an index
    Node sib = n;
    while ((sib = sib.getPreviousSibling()) != null) {
        if (name.equals(getLocalName(sib))) {
            i++;
        }
    }
    if (i > 1) {
        return name + "[" + i + "]";
    }

    sib = n;
    while ((sib = sib.getNextSibling()) != null) {
        if (name.equals(getLocalName(sib))) {
            return name + "[1]";
        }
    }

    return name;
}