Java XML Element Get getElement(Element element, boolean before)

Here you can find the source of getElement(Element element, boolean before)

Description

get Element

License

Open Source License

Parameter

Parameter Description
element a parameter
before true to search the element before, false for the element after

Return

the element before element

Declaration

public static Element getElement(Element element, boolean before) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2008-2013, Linagora//ww w. j  a  v a  2 s.c  o  m
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *       Linagora - initial API and implementation
 *******************************************************************************/

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    /**
     * @param element
     * @param before true to search the element before, false for the element after
     * @return the element before <i>element</i>
     */
    public static Element getElement(Element element, boolean before) {

        NodeList bas = element.getParentNode().getChildNodes();
        Element searchedElement = null;

        boolean found = false;
        for (int i = 0; i < bas.getLength() && (searchedElement == null || !found); i++) {

            if (element.equals(bas.item(i)))
                found = true;

            else if (bas.item(i) instanceof Element) {

                // We look for the element before - we didn't find the right element yet
                if (before && !found)
                    searchedElement = (Element) bas.item(i);

                // We look for the element after - we have to find the right element first
                else if (!before && found)
                    searchedElement = (Element) bas.item(i);
            }
        }

        return searchedElement;
    }
}

Related

  1. getElement(Element el, String label)
  2. getElement(Element el, String tagName, int index)
  3. getElement(Element el, String... path)
  4. getElement(Element elem, String name)
  5. getElement(Element elem, String path)
  6. getElement(Element element, String name)
  7. getElement(Element element, String name, int index)
  8. getElement(Element element, String tag)
  9. getElement(Element element, String tagName)