Java XML Element to String getString(Element element)

Here you can find the source of getString(Element element)

Description

get String

License

Open Source License

Declaration

public static String getString(Element element) 

Method Source Code

//package com.java2s;
/*/*from  www. ja v  a 2  s . co  m*/
 * SmartDoc : Ultimate document format based on XML
 *  Copyright (C) 1998  ASAMI, Tomoharu (tasami@ibm.net)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

import org.w3c.dom.*;

public class Main {
    public static String getString(Element element) {
        StringBuffer buffer = new StringBuffer();
        _getString(element, buffer);
        return (new String(buffer));
    }

    protected static void _getString(Element element, StringBuffer buffer) {
        NodeList list = element.getChildNodes();
        int size = list.getLength();
        for (int i = 0; i < size; i++) {
            Node node = list.item(i);
            switch (node.getNodeType()) {

            case Node.ELEMENT_NODE:
                _getString(element, buffer);
                break;
            case Node.TEXT_NODE:
                buffer.append(((Text) node).getData());
                break;
            }
        }
    }
}

Related

  1. extractTextElementValue(final Element textElement)
  2. getElementAsInputStream(Element xml)
  3. getElementText(Element e)
  4. getNamedElemXml(Element parent, String elementName)
  5. getString(Element el, String label)
  6. getString(Element element, String expr)
  7. getString(Element element, String name, String def)
  8. getString(Element root, String name)
  9. getString(Element xmlElement)