Android XML Element Child Get getListContent(Element listElement, String listItemName, String listItemNamespace)

Here you can find the source of getListContent(Element listElement, String listItemName, String listItemNamespace)

Description

Get list content for the given XML element, of the form content...

Declaration

public static ArrayList<String> getListContent(Element listElement,
        String listItemName, String listItemNamespace) 

Method Source Code

//package com.java2s;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

import java.util.ArrayList;

public class Main {
    /**//w  ww . j a  v a  2s. c  o m
     * Get list content for the given XML element, of the form <Element>content</Element>...
     */
    public static ArrayList<String> getListContent(Element listElement,
            String listItemName, String listItemNamespace) {
        if (listElement == null) {
            throw new IllegalArgumentException("listElement cannot be null");
        }
        if (listItemName == null || listItemName.length() == 0) {
            throw new IllegalArgumentException(
                    "listItemName cannot be null or empty");
        }
        if (listItemNamespace == null) {
            throw new IllegalArgumentException(
                    "listItemNamespace cannot be null");
        }
        ArrayList<String> listContent = new ArrayList<String>();
        for (Node childNode = listElement.getFirstChild(); childNode != null; childNode = childNode
                .getNextSibling()) {
            if (childNode.getNodeType() == Node.ELEMENT_NODE
                    && listItemName.equals(childNode.getLocalName())
                    && listItemNamespace
                            .equals(childNode.getNamespaceURI())) {
                listContent.add(getContent((Element) childNode));
            }
        }
        return listContent;
    }

    /**
     * Get the content value for the given XML element
     */
    public static String getContent(Element xmlElement) {
        if (xmlElement == null) {
            throw new IllegalArgumentException("xmlElement cannot be null");
        }
        StringBuilder content = new StringBuilder();
        for (Node childNode = xmlElement.getFirstChild(); childNode != null; childNode = childNode
                .getNextSibling()) {
            if (childNode.getNodeType() == Node.TEXT_NODE) {
                content.append(childNode.getNodeValue());
            }
        }
        return content.toString();
    }
}

Related

  1. locateElement(Element root, String tagName, String keyAttributeName, String keyAttributeValue)
  2. valueOfFirstDescendantWithName(Element element, String name)
  3. firstDescendantWithName(Element element, String name)
  4. findNextSiblingElement(Element current)
  5. GetElement(Element root, String namespaceUri, String name, boolean throwIfNotFound)
  6. getStringFromXml(Element validate, Boolean hasHeader)