Example usage for org.w3c.dom CharacterData getData

List of usage examples for org.w3c.dom CharacterData getData

Introduction

In this page you can find the example usage for org.w3c.dom CharacterData getData.

Prototype

public String getData() throws DOMException;

Source Link

Document

The character data of the node that implements this interface.

Usage

From source file:Main.java

public static String getContentText(Element element) {
    StringBuilder text = new StringBuilder();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child instanceof CharacterData) {
            CharacterData characterData = (CharacterData) child;
            text.append(characterData.getData());
        }//  w  w w .j  a  va 2  s.c o  m
    }
    return text.toString();
}

From source file:Main.java

/**
 * Returns the text content of the provided element as is.  If multiple text
 * DOM nodes are present, they are concatenated together. 
 * //from  ww  w. j  a  va2  s.  c  om
 * @param element The element that contains the desired text content.
 * @return The text content of the given element.
 * @throws Exception If an exception occurs during the traversal of the DOM
 * objects.
 */
public static String getElementTextData(Element element) throws Exception {
    if (!element.hasChildNodes()) {
        throw new Exception("Element has no children.");
    }

    Node n = element.getFirstChild();

    if ((n.getNodeType() != Node.TEXT_NODE) && (n.getNodeType() != Node.CDATA_SECTION_NODE)) {
        // must be a textual node
        // for now throw an exception, but later need to just skip this module and
        // resume initialization
        throw new Exception("Element child node is not textual.");
    }

    CharacterData value = (CharacterData) n;
    return value.getData();
}

From source file:Main.java

/**
 * Returns all child character data of the given element, including CDATA sections but NOT entity references.
 * @param parentEl the parent element.// w  ww  .  j a va 2 s  .  c  o m
 * @return the child character data of the element, or null if the element is null.
 */
static public String getChildCharacterData(Element parentEl) {
    if (parentEl == null) {
        return null;
    }

    Node tempNode = parentEl.getFirstChild();
    StringBuffer strBuf = new StringBuffer();
    CharacterData charData;

    while (tempNode != null) {
        switch (tempNode.getNodeType()) {
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
            charData = (CharacterData) tempNode;
            strBuf.append(charData.getData());
            break;
        //            case Node.ENTITY_REFERENCE_NODE : strBuf.append("&").append(tempNode.getNodeName()).append(";");

        }
        tempNode = tempNode.getNextSibling();
    }
    return strBuf.toString();
}

From source file:Main.java

/**
 * Concat all the text and cdata node children of this elem and return
 * the resulting text.//from   w ww. j av  a  2s. com
 * (by Matt Duftler)
 *
 * @param parentEl the element whose cdata/text node values are to
 *                 be combined.
 * @return the concatanated string.
 */
public static String getChildCharacterData(Element parentEl) {
    if (parentEl == null) {
        return null;
    }
    Node tempNode = parentEl.getFirstChild();
    StringBuffer strBuf = new StringBuffer();
    CharacterData charData;

    while (tempNode != null) {
        switch (tempNode.getNodeType()) {
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
            charData = (CharacterData) tempNode;
            strBuf.append(charData.getData());
            break;
        }
        tempNode = tempNode.getNextSibling();
    }
    return strBuf.toString();
}

From source file:com.microsoft.exchange.autodiscover.PoxAutodiscoverServiceImpl.java

/**
 * Static utility method to extract text from an XML element
 * @param e - an xml element/*ww  w.  j av a  2  s .c  o m*/
 * @return the character datafrom the first child element as a string
 */
private static String getCharacterDataFromElement(Element e) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
        CharacterData cd = (CharacterData) child;
        return cd.getData();
    }
    return null;
}

From source file:it.greenvulcano.gvesb.virtual.gv_multipart.MultipartCallOperation.java

/**
 * Get the text from a CDATA//  w  w  w  .  ja v a 2 s .co m
 * 
 * @param e
 * @return
 */
public static String getCharacterDataFromElement(Element element) {
    NodeList list = element.getChildNodes();
    String data;

    for (int index = 0; index < list.getLength(); index++) {
        if (list.item(index) instanceof CharacterData) {
            CharacterData child = (CharacterData) list.item(index);
            data = child.getData();
            if (data != null && data.trim().length() > 0)
                return child.getData();
        }
    }
    return "";
}

From source file:org.mobicents.charging.server.ratingengine.http.HTTPClientSbb.java

private String getCharacterDataFromElement(Element e) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
        CharacterData cd = (CharacterData) child;
        return cd.getData();
    }//w  w  w. j  ava 2  s . c o m
    return "?";
}

From source file:autohit.creator.compiler.SimCompiler.java

/**
 *  Get the text out of an XML node./*w w w  .  java2  s  .c  om*/
 *
 *  @param cdn XML node.
 *  @return the text.
 */
private String getText(Node cdn) {

    try {
        if ((cdn.getNodeType() == Node.TEXT_NODE) || (cdn.getNodeType() != Node.CDATA_SECTION_NODE)) {
            CharacterData cdnc = (CharacterData) cdn;
            return cdnc.getData();
        }
    } catch (Exception e) {
    } // ignore.  re are returning empty anyway
    return null;
}

From source file:org.apache.ode.utils.DOMUtils.java

/**
 * Concat all the text and cdata node children of this elem and return the
 * resulting text./*from  ww w  . jav a  2s.c o  m*/
 *
 * @param parentEl the element whose cdata/text node values are to be
 *        combined.
 *
 * @return the concatanated string.
 */
static public String getChildCharacterData(Element parentEl) {
    if (parentEl == null) {
        return null;
    }
    Node tempNode = parentEl.getFirstChild();
    StringBuffer strBuf = new StringBuffer();
    CharacterData charData;
    while (tempNode != null) {
        switch (tempNode.getNodeType()) {
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
            charData = (CharacterData) tempNode;
            strBuf.append(charData.getData());
            break;
        }
        tempNode = tempNode.getNextSibling();
    }
    return strBuf.toString();
}

From source file:org.getobjects.appserver.templates.WOxElemBuilder.java

public WOElement buildCharacterData(CharacterData _node, WOxElemBuilder _b) {
    if (_node == null)
        return null;

    String s = _node.getData();
    if (s == null)
        return null;
    if (s.length() == 0)
        return null;

    return new WOString(WOAssociation.associationWithValue(s), true /* escapeHTML */);
}