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 getCharacterDataFromElement(Element e) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
        CharacterData cd = (CharacterData) child;
        return cd.getData();
    }/* w w  w  .java2s .  co m*/
    return "";
}

From source file:Main.java

public static String getCharacterDataFromElement(Element e) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
        CharacterData cd = (CharacterData) child;
        return cd.getData();
    }//from  w  w  w. j a v a 2s.  c om
    return "?";
}

From source file:Main.java

/**
 * Gets String representation of XML tag value
 *//*  ww  w. java2  s .  c o  m*/
public static String getCharacterDataFromElement(Element line) {
    Node child = ((Node) line).getFirstChild();
    if (child instanceof CharacterData) {
        CharacterData cd = (CharacterData) child;
        return cd.getData();
    }
    return "?";
}

From source file:Main.java

@Deprecated
private static String getString(Node child) {
    String s = "";
    if (child instanceof CharacterData) {
        CharacterData cd = (CharacterData) child;
        s = cd.getData();
    }//from  ww  w  .ja va2 s. com
    while (s.startsWith("\n")) {
        s = s.substring(1);
    }

    return s;
}

From source file:Main.java

public static String getElementValue(Element parent, String label) {
    Element e = (Element) parent.getElementsByTagName(label).item(0);

    try {//from w  w w  .j  a  v a 2s .  c om
        Node child = e.getFirstChild();
        if (child instanceof CharacterData) {
            CharacterData cd = (CharacterData) child;
            return cd.getData();
        }
    } catch (Exception ex) {
    }
    return "";
}

From source file:Main.java

public static String getCharacterDataFromElement(Element e) {
    org.w3c.dom.Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
        CharacterData cd = (CharacterData) child;
        return cd.getData();
    }/*from ww w  .  j  av  a2 s .c o  m*/
    return "?";
}

From source file:Main.java

/**
 * Extracts the first CDATA child from the given {@code org.w3c.dom.Element}.
 *
 * @param elem the parent Element/*from  ww  w  .  jav  a2 s.  co  m*/
 * @return the String value of the CDATA section, or {@code null} if none
 *         found
 */
public static String getCdata(Element elem) {
    NodeList nodes = elem.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
            CharacterData cdataNode = (CharacterData) node;
            return cdataNode.getData();
        }
    }
    return null;
}

From source file:Main.java

static void printElement(Element element, String indent) {
    System.out.println("Element '" + element.getNodeName() + "'");
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        switch (child.getNodeType()) {
        case Node.ELEMENT_NODE:
            printElement((Element) child, indent + "\t");
            break;
        case Node.ATTRIBUTE_NODE:
            Attr attr = (Attr) child;
            System.out.println("\tAttribute: '" + attr.getName() + "' = '" + attr.getValue() + "'");
            break;
        case Node.COMMENT_NODE:
            Comment comment = (Comment) child;
            System.out.println("\tComment: '" + comment.getData() + "'");
            break;
        case Node.CDATA_SECTION_NODE:
            CharacterData cdata = (CharacterData) child;
            System.out.println("\tCDatat: '" + cdata.getData() + "'");
            break;
        case Node.TEXT_NODE:
            Text text = (Text) child;
            System.out.println("\tText: '" + text.getData() + "'");
            break;
        default://  ww w.  j  a  va2 s .  co m
            System.out.println("\tUnknown node type: '" + child.getNodeType() + "'");
            break;
        }
    }
}

From source file:Main.java

public static String getContentText(Element element) {
    StringBuffer buffer = new StringBuffer();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node instanceof CharacterData) {
            CharacterData characterData = (CharacterData) node;
            buffer.append(characterData.getData());
        }/*from   www. j  a v a2  s  .  com*/
    }
    return buffer.toString();
}

From source file:Main.java

public static String getContentText(Element element) {
    StringBuilder text = new StringBuilder();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node instanceof CharacterData) {
            CharacterData characterData = (CharacterData) node;
            text.append(characterData.getData());
        }/*from w  ww .j  a  v  a 2s  .c o  m*/
    }
    return text.toString();
}