Java Utililty Methods XML Element Get

List of utility methods to do XML Element Get

Description

The list of methods to do XML Element Get are organized into topic(s).

Method

ElementgetElementDescendant(Element elem, String ns_uri, String tagName)
get Element Descendant
NodeList nl = elem.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        if ((node.getNamespaceURI() == null || node.getNamespaceURI().equals(ns_uri))
                && node.getLocalName().equals(tagName)) {
            return (Element) node;
        } else {
...
ElementgetElementForLine(Document document, int line)
get Element For Line
Element[] roots = document.getRootElements();
Element root = roots[0];
return root.getElement(line - 1);
StringgetElementId(Element element)
get Element Id
return element.getAttribute("id");
intgetElementIndex(Element ele)
Returns the index of the Node within the current sub-tree.
int index = 1;
Node prev = ele.getPreviousSibling();
while (prev != null) {
    if (prev.getNodeType() == Node.ELEMENT_NODE) {
        if (((Element) prev).getNodeName().equals(ele.getNodeName())) {
            ++index;
    prev = prev.getPreviousSibling();
return index;
intgetElementIndex(Element element)
Returns the index of element in the list of all elements with the same name in its parent node.
int index = 1;
Node sibling = element;
while ((sibling = sibling.getPreviousSibling()) != null) {
    if (sibling instanceof Element) {
        Element siblingElement = (Element) sibling;
        if (element.getLocalName().equals(siblingElement.getLocalName())
                && (element.getNamespaceURI() == null ? siblingElement.getNamespaceURI() == null
                        : element.getNamespaceURI().equals(siblingElement.getNamespaceURI()))) {
...
StringgetElementLanguage(Element elemNode, String defaultLangCode)
Returns the language code associated with the specified element.
String langCode = defaultLangCode;
if (elemNode.hasAttribute(langAttName)) {
    langCode = elemNode.getAttribute(langAttName);
} else if (elemNode.hasAttributeNS("xml", "lang")) {
    langCode = elemNode.getAttributeNS("xml", "lang");
return langCode;
StringgetElementLanguage(Element elemNode, String defaultLangCode)
Returns the language code associated with the specified element.
String langCode = defaultLangCode;
if (elemNode.hasAttribute("language")) {
    langCode = elemNode.getAttribute("language");
} else if (elemNode.hasAttribute("lang")) {
    langCode = elemNode.getAttribute("lang");
} else if (elemNode.hasAttributeNS("xml", "lang")) {
    langCode = elemNode.getAttributeNS("xml", "lang");
return langCode;
ListgetElementList(Document doc, String expression)
get Element List
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
XPathExpression expr = xPath.compile(expression);
NodeList nodeList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
List<Element> elementList = new ArrayList<Element>();
for (int i = 0; i < nodeList.getLength(); i++) {
    Node item = nodeList.item(i);
    if (item instanceof Element) {
...
ListgetElementList(Element dataRoot, String name)
get Element List
NodeList list = dataRoot.getElementsByTagName(name);
List<Element> listElements = new ArrayList<Element>();
for (int i = 0; i < list.getLength(); i++) {
    listElements.add((Element) list.item(i));
return listElements;
ListgetElementList(Element dataRoot, String name)
get Element List
NodeList list = dataRoot.getElementsByTagName(name);
List<Element> listElements = new ArrayList<Element>();
for (int i = 0; i < list.getLength(); i++) {
    Element item = (Element) list.item(i);
    if (item.getParentNode().equals(dataRoot)) {
        listElements.add(item);
return listElements;