Example usage for org.w3c.dom Node TEXT_NODE

List of usage examples for org.w3c.dom Node TEXT_NODE

Introduction

In this page you can find the example usage for org.w3c.dom Node TEXT_NODE.

Prototype

short TEXT_NODE

To view the source code for org.w3c.dom Node TEXT_NODE.

Click Source Link

Document

The node is a Text node.

Usage

From source file:Main.java

public static Map<String, String> XmlAsMap(Node node) {
    Map<String, String> map = new HashMap<String, String>();
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node currentNode = nodeList.item(i);
        if (currentNode.hasAttributes()) {
            for (int j = 0; j < currentNode.getAttributes().getLength(); j++) {
                Node item = currentNode.getAttributes().item(i);
                if (item != null)
                    map.put(item.getNodeName(), prepare(item.getTextContent()));
            }/*  ww w.  j  ava 2 s.  com*/
        }
        if (currentNode.getFirstChild() != null) {
            if (currentNode.getFirstChild().getNodeType() == Node.ELEMENT_NODE) {
                map.putAll(XmlAsMap(currentNode));
            } else if (currentNode.getFirstChild().getNodeType() == Node.TEXT_NODE) {
                map.put(currentNode.getLocalName(), prepare(currentNode.getTextContent()));
            }
        }
    }
    return map;
}

From source file:Main.java

public static String getXpathExpressionValue(Object xprContext, String xpExpression)
        throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();

    XPathExpression xpe = xpath.compile(xpExpression);
    Node valueNode = (Node) xpe.evaluate(xprContext, XPathConstants.NODE);
    String value = null;/*w  ww  .j  av a 2 s  . c  o  m*/
    if (valueNode != null)
        value = valueNode.getNodeValue();
    if (value != null) {
        // if the node is a text node - then we trim and (potentially) look for CDATA
        if (valueNode.getNodeType() == Node.TEXT_NODE) {
            value = value.trim();

            // look for CDATA if we got nothing (stupid whitespace)
            if (value.length() == 0) {
                Node siblingForCDATA = valueNode.getNextSibling();
                if (siblingForCDATA.getNodeType() == Node.CDATA_SECTION_NODE) {
                    value = siblingForCDATA.getNodeValue();
                }
            }
        }
    }

    return value;
}

From source file:Main.java

/**
 * Return the text (node value) of the first node under this, works best if normalized.
 *///from   w  ww . j a  v  a 2s. c  o m
public static String elementValue(Element element) {
    if (element == null)
        return null;
    // make sure we get all the text there...
    element.normalize();
    Node textNode = element.getFirstChild();

    if (textNode == null)
        return null;

    StringBuffer valueBuffer = new StringBuffer();
    do {
        if (textNode.getNodeType() == Node.CDATA_SECTION_NODE || textNode.getNodeType() == Node.TEXT_NODE) {
            valueBuffer.append(textNode.getNodeValue());
        }
    } while ((textNode = textNode.getNextSibling()) != null);
    return valueBuffer.toString();
}

From source file:Main.java

/**
 * Returns the child text from a DOM node.
 * @param node the node to parse//from  ww  w. j a  va 2  s . c  o m
 * @return the node text, or <tt>null</tt> if the node did not contain any text
 */
public static String getText(Node node) {
    StringBuilder s = null;
    Node child = node.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.TEXT_NODE) {
            if (s == null) {
                s = new StringBuilder();
            }
            s.append(((Text) child).getTextContent());
        } else if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
            if (s == null) {
                s = new StringBuilder();
            }
            s.append(((CDATASection) child).getData());
        }
        child = child.getNextSibling();
    }
    return s == null ? null : s.toString();
}

From source file:Main.java

/**
 * @param node//w w  w . ja  va  2 s. c om
 * @return true if the given node is of type text or CDATA.
 */
public static boolean isText(Node node) {
    int ntype = node.getNodeType();
    return ntype == Node.TEXT_NODE || ntype == Node.CDATA_SECTION_NODE;
}

From source file:MainClass.java

public static void print(Node node, OutputStream os) {
    PrintStream ps = new PrintStream(os);
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        ps.print("<" + node.getNodeName());

        NamedNodeMap map = node.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            ps.print(" " + map.item(i).getNodeName() + "=\"" + map.item(i).getNodeValue() + "\"");
        }/* ww w  . jav  a  2  s . c o  m*/
        ps.println(">");
        return;
    case Node.ATTRIBUTE_NODE:
        ps.println(node.getNodeName() + "=\"" + node.getNodeValue() + "\"");
        return;
    case Node.TEXT_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.CDATA_SECTION_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.PROCESSING_INSTRUCTION_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
        ps.println(node.getNodeName() + "=" + node.getNodeValue());
        return;
    }
}

From source file:Main.java

/**
 * Set the text of the specified element to the given string.
 * //from  w  w w . ja va 2  s.c  om
 * @param e The element.
 * @param text The text string.
 */
public static void setText(Element e, String text) {
    NodeList lst = e.getChildNodes();
    int size = lst.getLength();
    for (int i = 0; i < size; i++) {
        Node n = lst.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            Text t = (Text) n;
            t.setData(text.trim());
            return;
        }
    }
    Document doc = e.getOwnerDocument();
    // bit of a hack - we preserve the cdata on the way in so we can serialize correctly
    // This only works on xml to xml 
    // TODO need to have a "preserve format" or some such on the mdmi structure
    if (text.startsWith("<![CDATA[") && text.endsWith("]]>")) {
        CDATASection cdata = doc
                .createCDATASection(text != null ? text.substring(9, text.lastIndexOf("]]>")) : null);
        e.appendChild(cdata);
    } else {
        Text txt = doc.createTextNode(text != null ? text.trim() : null);
        e.appendChild(txt);
    }

}

From source file:Utils.java

/**
 * <p>Returns an array of text values of a child element. Returns
 * <code>null</code> if there is no child element found.</p>
 *
 * @param parent parent element//from   w  ww .  j  a  v a2s . c om
 * @param name name of the child element
 * @return text value
 */
public static String[] getChildElementTextArr(Element parent, String name) {
    // Get all the elements
    List children = getChildElementsByName(parent, name);

    String str[] = new String[children.size()];

    for (int i = 0; i < children.size(); i++) {
        Node child = (Node) children.get(i);

        StringBuffer buf = new StringBuffer();

        NodeList nodes = child.getChildNodes();
        for (int j = 0; j < nodes.getLength(); j++) {
            Node node = nodes.item(j);
            if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {
                Text text = (Text) node;
                buf.append(text.getData().trim());
            }
        }

        str[i] = buf.toString();
    }

    return str;
}

From source file:Main.java

public static int outputNode(Node outputNode, PrintWriter outputWriter, int curPos) {
    NodeList nodes = outputNode.getChildNodes();
    int curNodeNum;
    if (outputNode.getNodeType() == Node.TEXT_NODE) {
        outputWriter.print(outputNode.getNodeValue());
    } else {//w  w w. ja  va2 s  . com
        if (outputNode.getNodeName().equals("p")) {
            outputWriter.println();
        }

        for (curNodeNum = 0; curNodeNum < nodes.getLength(); curNodeNum++) {
            Node curNode = nodes.item(curNodeNum);
            curPos = outputNode(curNode, outputWriter, curPos);
        }

    }
    return (curPos);
}

From source file:Main.java

/** Goes through and adds newlines and indent to the current node and all its children
 * @param current the current node/*from   www.  ja va 2 s  .  c  o m*/
 * @param indent the current level of indent this is increased recursively*/
private static void addFormatting(Document doc, Node current, String indent) {

    // go through each of the children adding space as required
    Node child = current.getFirstChild();
    String childIndent = indent + "\t";
    while (child != null) {
        Node nextChild = child.getNextSibling();
        if (child.getNodeType() != Node.TEXT_NODE) {
            // Only if we aren't a text node do we add the space
            current.insertBefore(doc.createTextNode("\n" + childIndent), child);
            if (child.hasChildNodes()) {
                addFormatting(doc, child, childIndent);
            }
            if (nextChild == null) {
                // Because this is the last child, we need to add some space after it
                current.appendChild(doc.createTextNode("\n" + indent));
            }
        }
        child = nextChild;
    }
}