Java XML Element Get getElementContent(Element elem)

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

Description

Returns the string content of an element (e.g., xsl:value-of()).

License

Open Source License

Parameter

Parameter Description
elem Element to get the value of.

Declaration

public static String getElementContent(Element elem) 

Method Source Code

//package com.java2s;
/*--------------------------------------------------------------------------------
 Copyright (C) 2002, 2004 ISOGEN International

 http://www.isogen.com/*  w ww .j a v  a 2 s  .  co m*/

 This program 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.

 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 Lesser 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.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Attr;

public class Main {
    /**
     * Returns the string content of an element (e.g., xsl:value-of()).
     * @param elem Element to get the value of.
     */
    public static String getElementContent(Element elem) {
        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);
                    resultStr = resultStr + echoEndTag((Element) child);
                } else if (child.getNodeType() == Node.TEXT_NODE) {
                    resultStr = resultStr + ((Text) child).getData();
                } // Else: ignore other node types
            }
        }
        return resultStr;
    }

    /**
     * Given an element, returns the start tag as a string, including any attributes.
     * 
     * Used to generate markup strings from elements.
     * 
     * NOTE: This implementation does not account for '"' characters within attribute
     * values.
     * @param elem The element node to be echoed.
     * @return XML start tag.
     */
    public static String echoStartTag(Element elem) {
        String resultStr = "<" + elem.getTagName();
        String attstr = "";
        NamedNodeMap atts = elem.getAttributes();
        for (int j = 0; j < atts.getLength(); j++) {
            Attr att = (Attr) (atts.item(j));
            // FIXME: doesn't account for '"' within attribute value.
            attstr = attstr + " " + att.getNodeName() + "=\""
                    + att.getNodeValue() + "\"";
        }
        resultStr = resultStr + attstr + ">";
        return resultStr;
    }

    /**
     * Given an element, returns the end tag as a string.
     * 
     * Used to generate markup strings from elements.
     * 
     * @param elem The element node to be echoed.
     * @return XML end tag.
     */
    public static String echoEndTag(Element elem) {
        return "</" + elem.getTagName() + ">";
    }
}

Related

  1. getElementByTagName(Element element, String tag)
  2. getElementByTagName(Element element, String tag)
  3. getElementByTagName(Element n, String elementName)
  4. getElementContent(Element e)
  5. getElementContent(Element elem)
  6. getElementContent(Element element, String defaultStr)
  7. getElementContent(Element element, String defaultStr)
  8. getElementContent(final Element el)
  9. getElementContent(final Element element)