Java XML Element Text getElementTextValue(Element in)

Here you can find the source of getElementTextValue(Element in)

Description

retrieve text from text from element

License

Apache License

Parameter

Parameter Description
in non-null Element

Return

possibly null text string

Declaration

public static String getElementTextValue(Element in) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.*;

import java.util.*;

public class Main {
    /**//from ww  w. j  a v a  2  s . com
    * retrieve text from text from element
    * @param in non-null Element
    * @return possibly null text string
    */
    public static String getElementTextValue(Element in) {
        Node[] testItems = getSubnodesOfType(in, Node.TEXT_NODE);
        if (testItems.length > 0)
            return (testItems[0].getNodeValue());
        testItems = getSubnodesOfType(in, Node.CDATA_SECTION_NODE);
        if (testItems.length > 0)
            return (testItems[0].getNodeValue());
        return (null);
    }

    /**
    * @param in non-null Node
    * @param type Some note type constant i.e. Node.CDATA_SECTION_NODE
    * @return non-null array or children of that type
    */
    public static Node[] getSubnodesOfType(Node in, int type) {
        NodeList subnodes = in.getChildNodes();
        if (subnodes == null)
            return (new Node[0]);
        int count = subnodes.getLength();
        List holder = new ArrayList();
        for (int i = 0; i < count; i++) {
            Node test = subnodes.item(i);
            if (test.getNodeType() == type)
                holder.add(test);
        }
        Node[] ret = new Node[holder.size()];
        holder.toArray(ret);
        return (ret);
    }
}

Related

  1. getElementText(final Element element)
  2. getElementTextByTagName(Element n, String elementName)
  3. getElementTextDataNoEx(Element element, boolean unindent)
  4. getElementTextOrNull(final XMLStreamReader reader)
  5. getElementTextValue(Element element)
  6. getSimpleElementText(Element element)
  7. getSimpleElementText(Element node)
  8. getSimpleElementText(Element node)
  9. getSimpleElementText(Element node, boolean trim)