Example usage for org.w3c.dom Element getNextSibling

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

Introduction

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

Prototype

public Node getNextSibling();

Source Link

Document

The node immediately following this node.

Usage

From source file:DOMUtils.java

/**
 * Return the next sibling element of the given element. Null if no
 * more sibling elements are found./*from  www.j ava2  s .co  m*/
 *
 * @param elem Element whose sibling element is to be returned
 * @return the next sibling element.
 */
public static Element getNextSiblingElement(Element elem) {
    for (Node n = elem.getNextSibling(); n != null; n = n.getNextSibling()) {
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) n;
        }
    }
    return null;
}

From source file:DOMEdit.java

public static void newEmail(Document doc, String newname, String newemail) {
        Element root = doc.getDocumentElement();
        Element person = (Element) root.getFirstChild();
        while (person != null) {
            Element name = (Element) person.getFirstChild();
            Text nametext = (Text) name.getFirstChild();
            String oldname = nametext.getData();
            if (oldname.equals(newname)) {
                Element email = (Element) name.getNextSibling();
                Text emailtext = (Text) email.getFirstChild();
                emailtext.setData(newemail);
            }//from  www . j a  v  a 2s  .c om
            person = (Element) person.getNextSibling();
        }
    }

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

/**
 * Get the next sibling that is an element
 * @param elem the element/*from w  ww . java  2 s  .co m*/
 * @param skipText if true skip text nodes to next element node
 * @return its next sibling of elem or null
 */
static Element nextSibling(Element elem, boolean skipText) {
    Node n = elem.getNextSibling();
    while (n != null) {
        if (!skipText && n.getNodeType() == Node.TEXT_NODE && !isWhitespace(n.getTextContent()))
            return null;
        else if (n.getNodeType() == Node.ELEMENT_NODE)
            return (Element) n;
        n = n.getNextSibling();
    }
    return null;
}

From source file:XMLUtils.java

/**
 * Get the next sibling element of a given element.
 * @param el//  w ww. j a v a 2  s .co  m
 * @return
 */
public static Element getNext(Element el) {
    Node n = el.getNextSibling();
    while (n != null && !(n instanceof Element)) {
        // get the next one
        n = n.getNextSibling();
    }

    if (n instanceof Element) {
        return (Element) n;
    }
    // else, nothing to return
    return null;
}

From source file:XMLUtils.java

/**
 * Get the next sibling element of a given element.
 * @param el//w w  w  .j a v a2 s .co  m
 * @return
 */
public static Element getNextSibling(Element el) {
    String tagName = el.getTagName();
    if (tagName == null) {
        return null;
    }
    Node n = el.getNextSibling();
    while (n != null && (!(n instanceof Element) || !tagName.equals(((Element) n).getTagName()))) {
        // get the next one
        n = n.getNextSibling();
    }

    if (n instanceof Element) {
        return (Element) n;
    } else {
        // else, nothing to return
        return null;
    }
}

From source file:be.fedict.eid.dss.spi.utils.XAdESUtils.java

/**
 * Find the next sibling at DOM level of the given XAdES DOM element.
 * //from   w ww . j a  v  a 2 s .c o  m
 * @param xadesElement
 * @param namespace
 * @param localName
 * @param jaxbType
 * @return
 * @throws XAdESValidationException
 */
public static <T> T findNextSibling(Element xadesElement, String namespace, String localName, Class<T> jaxbType)
        throws XAdESValidationException {
    Node siblingNode = xadesElement.getNextSibling();
    while (siblingNode != null && siblingNode.getNodeType() != Node.ELEMENT_NODE) {
        /*
         * Can happen as shown during latest ETSI XAdES plugtests.
         */
        LOG.debug("skipping a non-Element sibling: " + siblingNode.getNodeType());
        if (Node.TEXT_NODE == siblingNode.getNodeType()) {
            LOG.debug("TEXT node sibling: \"" + siblingNode.getNodeValue() + "\"");
        }
        siblingNode = siblingNode.getNextSibling();
    }
    if (null == siblingNode) {
        return null;
    }
    Element element = (Element) siblingNode;
    if (false == namespace.equals(element.getNamespaceURI())) {
        return null;
    }
    if (false == localName.equals(element.getLocalName())) {
        return null;
    }
    return unmarshall(element, jaxbType);
}

From source file:com.enonic.esl.xml.XMLTool.java

public static void removeAllSiblings(Element node) {

    Element parent = (Element) node.getParentNode();

    Element sibling = (Element) parent.getFirstChild();
    while (sibling != null) {
        if (sibling != node) {
            sibling = removeChildFromParent(parent, sibling);
        } else {/*from w  w  w .  j a  v  a  2s  .  co m*/
            sibling = (Element) sibling.getNextSibling();
        }
    }
}

From source file:com.wfreitas.camelsoap.SoapClient.java

/**
 * Clone a collection node./*from  w w  w  . j  a v a2 s.  c  o m*/
 * <p/>
 * Note we have to frig with the OGNL expressions for collections/arrays because the
 * collection entry is represented by [0], [1] etc in the OGNL expression, not the actual
 * element name on the DOM e.g. collection node "order/items/item" (where "item" is the
 * actual collection entry) maps to the OGNL expression "order.items[0]" etc.
 *
 * @param element    The collection/array "entry" sub-branch.
 * @param cloneCount The number of times it needs to be cloned.
 * @param ognl       The OGNL expression for the collection/array. Not including the
 *                   indexing part.
 */
private void cloneCollectionTemplateElement(Element element, int cloneCount, String ognl) {
    if (element == null) {
        return;
    }

    Node insertPoint = element.getNextSibling();
    Node parent = element.getParentNode();

    element.setAttributeNS(OGNLUtils.JBOSSESB_SOAP_NS,
            OGNLUtils.JBOSSESB_SOAP_NS_PREFIX + OGNLUtils.OGNL_ATTRIB, ognl + "[0]");
    for (int i = 0; i < cloneCount; i++) {
        Element clone = (Element) element.cloneNode(true);

        clone.setAttributeNS(OGNLUtils.JBOSSESB_SOAP_NS, OGNLUtils.JBOSSESB_SOAP_NS_PREFIX + IS_CLONE_ATTRIB,
                "true");
        clone.setAttributeNS(OGNLUtils.JBOSSESB_SOAP_NS,
                OGNLUtils.JBOSSESB_SOAP_NS_PREFIX + OGNLUtils.OGNL_ATTRIB,
                ognl + "[" + Integer.toString(i + 1) + "]");
        if (insertPoint == null) {
            parent.appendChild(clone);
        } else {
            parent.insertBefore(clone, insertPoint);
        }
    }
}

From source file:com.alfaariss.oa.util.configuration.ConfigurationManager.java

/**
 * Resolve the next section.//w ww  .  jav a 2  s.c om
 * @see IConfigurationManager#getNextSection(org.w3c.dom.Element)
 */
public synchronized Element getNextSection(Element eSection) {
    if (eSection == null)
        throw new IllegalArgumentException("Suplied section is empty");

    String sRequested = eSection.getNodeName();
    //Get first
    Node nNext = eSection.getNextSibling();
    while (nNext != null) //No more sections
    {
        if (nNext.getNodeType() == Node.ELEMENT_NODE && nNext.getNodeName().equals(sRequested))
            break;
        nNext = nNext.getNextSibling(); //Get next
    }
    return (Element) nNext;
}

From source file:com.enonic.esl.xml.XMLTool.java

/**
 * Removes a child element from a parent element, and returns the next element to the removed one.
 *
 * @param parent The parent element to the child to be removed.
 * @param child  The child element to remove.
 * @return The next element to the removed child.
 *///w ww.j av a 2s .c  om
public static Element removeChildFromParent(Element parent, Element child) {

    Element previousChild = (Element) child.getPreviousSibling();

    // If the child to remove is the first child
    if (previousChild == null) {
        parent.removeChild(child);
        // Return the first child as the next child
        return (Element) parent.getFirstChild();
    }
    // If the child is not the first child
    else {
        parent.removeChild(child);
        return (Element) previousChild.getNextSibling();
    }
}