Java XML Element Get Value getText(Element elem)

Here you can find the source of getText(Element elem)

Description

Extract the text in an element.

License

Open Source License

Declaration

public static String getText(Element elem) 

Method Source Code


//package com.java2s;
/*  /*  w w  w  . jav a2s .  co  m*/
 * XMLUtils.java
 *
 * Copyright (C) August Mayer, 2001-2004. All rights reserved.
 * Please consult the Boone LICENSE file for additional rights granted to you.
 *
 * Created on 09. Dezember 2002, 16:31
 */

import org.w3c.dom.*;

public class Main {
    /**
     * Extract the text in an element.
     * Concats data of Text and CDATASection elements.
     */
    public static String getText(Element elem) {

        StringBuffer text = new StringBuffer();
        NodeList l = elem.getChildNodes();
        for (int i = 0; i < l.getLength(); i++) {
            Node n = l.item(i);
            if (n instanceof Text)
                text.append(((Text) n).getData());
            else if (n instanceof CDATASection)
                text.append(((CDATASection) n).getData());
        }
        return text.toString();
    }
}

Related

  1. getText(Element config)
  2. getText(Element config)
  3. getText(Element e)
  4. getText(Element el)
  5. getText(Element elem)
  6. getText(Element elem)
  7. getText(Element elem)
  8. getText(Element elem, String ifnull)
  9. getText(Element elem, String name)