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

ElementgetElementByTagName(Element n, String elementName)
get Element By Tag Name
int sz = n.getChildNodes().getLength();
ArrayList<Element> elements = new ArrayList<Element>(sz);
for (int idx = 0; idx < sz; idx++) {
    Node node = n.getChildNodes().item(idx);
    if (node instanceof Element && node.getLocalName().equals(elementName))
        elements.add((Element) node);
if (elements.size() > 0)
...
StringgetElementContent(Element e)
Returns the content of the given element.
StringBuffer result = new StringBuffer();
for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) {
    switch (n.getNodeType()) {
    case Node.ELEMENT_NODE:
        result.append(getElementContent((Element) n));
        break;
    case Node.CDATA_SECTION_NODE:
    case Node.TEXT_NODE:
...
StringgetElementContent(Element elem)
Returns the string content of an element (e.g., xsl:value-of()).
String resultStr = "";
if (elem != null) {
    NodeList childs = elem.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
        Node child = childs.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            resultStr = resultStr + echoStartTag((Element) child);
            resultStr = resultStr + getElementContent((Element) child);
...
StringgetElementContent(Element elem)
Returns the string content of an element (e.g., xsl:value-of() except that the markup of subelements is included in the string).
String resultStr = "";
if (elem != null) {
    NodeList childs = elem.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
        Node child = childs.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            resultStr = resultStr + echoStartTag((Element) child);
            resultStr = resultStr + getElementContent((Element) child);
...
StringgetElementContent(Element element, String defaultStr)
Get the content of the given element.
if (element == null)
    return defaultStr;
NodeList children = element.getChildNodes();
String result = "";
for (int i = 0; i < children.getLength(); i++) {
    if (children.item(i).getNodeType() == Node.TEXT_NODE
            || children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) {
        result += children.item(i).getNodeValue();
...
StringgetElementContent(Element element, String defaultStr)
Get the content of the given element.
if (element == null) {
    return defaultStr;
NodeList children = element.getChildNodes();
StringBuilder result = new StringBuilder("");
for (int i = 0; i < children.getLength(); i++) {
    if (children.item(i).getNodeType() == Node.TEXT_NODE
            || children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) {
...
StringgetElementContent(final Element el)
Return the content for the current element.
return getElementContent(el, true);
StringgetElementContent(final Element element)
Get the content of the given element.
return getElementContent(element, null);
StringgetElementContent(final Element element, final boolean trim)
get Element Content
final NodeList nl = element.getChildNodes();
String attributeText = "";
for (int i = 0; i < nl.getLength(); i++) {
    final Node n = nl.item(i);
    if (n instanceof Text) {
        attributeText += ((Text) n).getData();
if (trim) {
    attributeText = attributeText.trim();
return attributeText;
StringgetElementContents(Element element, String elementName)
get Element Contents
Element first = getFirstElement(element, elementName);
if (first != null) {
    return first.getTextContent().trim();
} else {
    return null;