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

/**
 * Get the text content of an element./*from   w  ww .jav  a 2  s.  com*/
 *
 * @param element
 *            The element.
 * @param sbuf
 *            The buffer to append to.
 * @param decend
 *            Whether to descend into child elements.
 */
public static void getText(final Element element, final StringBuilder sbuf, final boolean decend) {
    Node node = element.getFirstChild();

    while (node != null) {
        switch (node.getNodeType()) {
        case Node.TEXT_NODE:
            sbuf.append(node.getNodeValue());
            break;

        case Node.ELEMENT_NODE:
            if (decend) {
                getText((Element) node, sbuf, decend);
            }

            break;
        }

        node = node.getNextSibling();
    }
}

From source file:Main.java

private static int findNodeIndex(Node node) {
    String nm = node.getLocalName();
    String ns = node.getNamespaceURI();
    short nt = node.getNodeType();

    Node parentNode = node.getParentNode();
    if (parentNode.getNodeType() != Node.ELEMENT_NODE)
        return 1;

    Node child = parentNode.getFirstChild();

    int ix = 0;/*w w w.j  a  va  2  s  .  com*/
    while (child != null) {
        if (child == node)
            return ix + 1;

        if (child.getNodeType() == nt && nm.equals(child.getLocalName())
                && ((ns == null && child.getNamespaceURI() == null)
                        || (ns != null && ns.equals(child.getNamespaceURI()))))
            ix++;

        child = child.getNextSibling();
    }

    throw new RuntimeException("Child node not found in parent!?");
}

From source file:Main.java

/**
 * Checks whether the expected XML element is contained in the actual
 * element. I.e., every attribute of the expected element must occur in the
 * actual element, with the same value, and all child nodes in the expected
 * element must occur in the actual element; if multiple child nodes with
 * the same name are found in the expected element, the first found
 * identifying attribute in the expected child element is used to identify
 * the child in the actual element./*from  ww  w.j ava  2 s .co  m*/
 * 
 * @param expected
 * @param actual
 * @return
 */
public static void assertContains(Element expected, Element actual, String messagePrefix,
        String... identifyingAttributes) {
    if (!expected.getTagName().equals(actual.getTagName())) {
        throw new AssertionError(messagePrefix + "\nExpected tagname differs from actual tagname (expected '"
                + printNodeOnly(expected) + "', found " + printNodeOnly(actual) + ")");
    }

    // Compare attributes
    NamedNodeMap expectedAttributes = expected.getAttributes();
    for (int i = expectedAttributes.getLength() - 1; i >= 0; i--) {
        String expectedAttributeName = expectedAttributes.item(i).getNodeName();
        String expectedAttributeValue = expectedAttributes.item(i).getNodeValue();

        String actualValue = actual.getAttribute(expectedAttributeName);
        if (actualValue == null || actualValue.isEmpty()) {
            throw new AssertionError(messagePrefix + "\nThe attribute '" + expectedAttributeName
                    + "' with value '" + expectedAttributeValue + "' was not found in the result "
                    + printNodeOnly(actual));
        }
        if (!expectedAttributeValue.equals(actualValue)) {
            throw new AssertionError(messagePrefix + "\nThe attribute '" + expectedAttributeName
                    + "' has value '" + actualValue + "' instead of '" + expectedAttributeValue
                    + "' in the result " + printNodeOnly(actual));
        }
    }

    // Compare child elements
    Node child = expected.getFirstChild();
    while (child != null) {
        if (child instanceof Element) {
            assertContainsChildElement((Element) child, actual, messagePrefix, identifyingAttributes);
        }
        child = child.getNextSibling();
    }
}

From source file:importer.handler.post.stages.Discriminator.java

/**
 * Get the next child that is an element
 * @param elem the element//from  w  ww .java 2  s . co m
 * @return its first child of elem or null
 */
static Element firstChild(Element elem) {
    Node n = elem.getFirstChild();
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
        n = n.getNextSibling();
    return (Element) n;
}

From source file:Main.java

/**
 * Retrieves the text of a given element.
 * /*  w  w w  .j a v  a  2  s. c o m*/
 * @param elem the Element for which the text value is requested
 * @return the text value of that element or null if the element has no text value
 */
static public String getElementText(Node elem) {
    String value = null;
    Node node = (elem != null) ? elem.getFirstChild() : null;
    // Find Text
    while (node != null) { // Find all Text nodes
        if (node.getNodeType() == Node.TEXT_NODE) { // set or append
            if (value == null)
                value = node.getNodeValue();
            else
                value += node.getNodeValue();
        }
        node = node.getNextSibling();
    }
    return value;
}

From source file:Main.java

/**
 * Concat all the text and cdata node children of this elem and return
 * the resulting text./* w  w w  .  ja  va2  s .c  o  m*/
 * (by Matt Duftler)
 *
 * @param parentEl the element whose cdata/text node values are to
 *                 be combined.
 * @return the concatanated string.
 */
public static String getChildCharacterData(Element parentEl) {
    if (parentEl == null) {
        return null;
    }
    Node tempNode = parentEl.getFirstChild();
    StringBuffer strBuf = new StringBuffer();
    CharacterData charData;

    while (tempNode != null) {
        switch (tempNode.getNodeType()) {
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
            charData = (CharacterData) tempNode;
            strBuf.append(charData.getData());
            break;
        }
        tempNode = tempNode.getNextSibling();
    }
    return strBuf.toString();
}

From source file:Main.java

/**
 * Dumps a debug listing of the child nodes of the given node to
 * System.out.//  w  w w.j  a  v  a2 s .  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:Main.java

private static void getChildrenText(NodeList nodeList, StringBuffer buf) {
    int len = nodeList.getLength();
    for (int i = 0; i < len; ++i) {
        Node child = nodeList.item(i);
        while (child != null) {
            short nodeType = child.getNodeType();
            switch (nodeType) {
            case Node.TEXT_NODE:
                buf.append(child.getNodeValue());
                break;
            case Node.ELEMENT_NODE:
                getChildrenText(child.getChildNodes(), buf);
                break;
            }//from   w  ww .  j  a v  a 2s  . c o m
            child = child.getNextSibling();
        }
    }
}

From source file:com.jaspersoft.jasperserver.ws.xml.Unmarshaller.java

static public String readPCDATA(Node textNode, boolean trim) {
    NodeList list_child = textNode.getChildNodes();
    for (int ck = 0; ck < list_child.getLength(); ck++) {
        Node child_child = (Node) list_child.item(ck);

        // --- start solution: if there is another node this should be the PCDATA-node
        Node ns = child_child.getNextSibling();
        if (ns != null)
            child_child = ns;//from   ww  w.  j ava 2s .c  om
        // --- end solution

        final short nt = child_child.getNodeType();

        // 1. look for a CDATA first...
        if (nt == Node.CDATA_SECTION_NODE) {
            if (trim)
                return ((String) child_child.getNodeValue()).trim();
            return (String) child_child.getNodeValue();
        }
    }

    for (int ck = 0; ck < list_child.getLength(); ck++) {
        Node child_child = (Node) list_child.item(ck);

        // --- start solution: if there is another node this should be the PCDATA-node
        Node ns = child_child.getNextSibling();
        if (ns != null)
            child_child = ns;
        // --- end solution

        final short nt = child_child.getNodeType();
        // 1. look for a CDATA first...
        if (nt == Node.TEXT_NODE) {
            if (trim)
                return ((String) child_child.getNodeValue()).trim();
            return (String) child_child.getNodeValue();
        }
    }

    return "";
}

From source file:XMLReader.java

/** Returns element value
 * @param elem element (it is XML tag)/*from   w w w . j a va 2  s. c  o m*/
 * @return Element value otherwise empty String
 */
private final static String getElementValue(Node elem) {
    Node kid;
    if (elem != null) {
        if (elem.hasChildNodes()) {
            for (kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling()) {
                if (kid.getNodeType() == Node.TEXT_NODE) {
                    return kid.getNodeValue();
                }
            }
        }
    }
    return "";
}