Here you can find the source of getPrecedingSiblings(Element element, String namespaceUri)
Parameter | Description |
---|---|
element | The parent element. |
namespaceUri | The namespace that the childen must belong to. |
public static Element[] getPrecedingSiblings(Element element, String namespaceUri)
//package com.java2s; /*/*from w ww.j av a 2 s. c om*/ * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ import java.util.ArrayList; import java.util.List; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class Main { /** * Returns all preceding sibling elements of an element that belong to a * certain namespace. * @param element The parent element. * @param namespaceUri The namespace that the childen must belong to. * @return The preceding sibling elements. */ public static Element[] getPrecedingSiblings(Element element, String namespaceUri) { return getPrecedingSiblings(element, namespaceUri, "*"); } /** * Returns all preceding sibling elements of an element that belong to a * certain namespace. and have a certain local name. * @param element The parent element. * @param namespaceUri The namespace that the childen must belong to. * @param localName The local name of the children. * @return The preceding sibling elements. */ public static Element[] getPrecedingSiblings(Element element, String namespaceUri, String localName) { List childElements = new ArrayList(); Element parent = (Element) element.getParentNode(); Element[] children = getChildren(parent, namespaceUri, localName); int i = 0; while (children[i] != element && i < children.length) { childElements.add(children[i]); i++; } return (Element[]) childElements.toArray(new Element[childElements.size()]); } /** * Returns all child elements of an element, regardless of the namespace. * @param element The parent element. * @return The child elements. */ public static Element[] getChildren(Element element) { List childElements = new ArrayList(); NodeList children = element.getElementsByTagName("*"); for (int i = 0; i < children.getLength(); i++) { if (children.item(i).getParentNode() == element) { childElements.add(children.item(i)); } } return (Element[]) childElements.toArray(new Element[childElements.size()]); } /** * Returns all child elements of an element that belong to a certain * namespace. * @param element The parent element. * @param namespaceUri The namespace that the childen must belong to. * @return The child elements. */ public static Element[] getChildren(Element element, String namespaceUri) { return getChildren(element, namespaceUri, "*"); } /** * Returns all child elements of an element that belong to a certain * namespace and have a certain local name. * @param element The parent element. * @param namespaceUri The namespace that the childen must belong to. * @param localName The local name of the children. * @return The child elements. */ public static Element[] getChildren(Element element, String namespaceUri, String localName) { List childElements = new ArrayList(); NodeList children = element.getElementsByTagNameNS(namespaceUri, localName); for (int i = 0; i < children.getLength(); i++) { if (children.item(i).getParentNode() == element) { childElements.add(children.item(i)); } } return (Element[]) childElements.toArray(new Element[childElements.size()]); } }