Java XML NodeList getTextFields(NodeList list)

Here you can find the source of getTextFields(NodeList list)

Description

Extracts all text values from a list of Element nodes.

License

Open Source License

Declaration

public static String getTextFields(NodeList list) 

Method Source Code

//package com.java2s;
/**/*from ww w .  j  av a2 s  . c  o  m*/
 * Jafer Poject.
 * Copyright (C) 2005, Jafer Project, Oxford University.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Extracts all text values from a list of Element nodes. Appends them with
     * '\n' preceding
     */
    public static String getTextFields(NodeList list) {
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < list.getLength(); i++) {
            Node node = (Node) list.item(i);
            if (i > 0)
                sb.append("\n");
            sb.append(getTextValue(node));
        }
        return sb.toString();
    }

    /**
     * Get the text node from an element node.
     */
    public static String getTextValue(Node node) {
        StringBuffer sb = new StringBuffer("");
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            if (list.item(i).getNodeType() == Node.TEXT_NODE) {
                sb.append(list.item(i).getNodeValue());
            }
        }
        //System.out.println("returning:"+sb.toString());
        return sb.toString();
    }
}

Related

  1. getNodeValue(String tagName, NodeList nodes)
  2. getOccurs(String nodeName, NodeList nodes)
  3. getTableIDOfTableAlias(String tableAlias, NodeList referList1, NodeList referList2)
  4. getText(NodeList elem)
  5. getTextContent(NodeList nodeList)
  6. getXmlNodeValue(NodeList nodeList)
  7. hasElementNodes(NodeList list)
  8. hasImageNodes(NodeList nodeList)
  9. indexOf(NodeList nodeList, String tagName, int startIdx)