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:com.jaspersoft.jasperserver.ws.xml.Unmarshaller.java

static public String readPCDATA(Node textNode, boolean trim) {
    NodeList list_child = textNode.getChildNodes();
    for (int ck = 0; ck < list_child.getLength(); ck++) {
        Node child_child = (Node) list_child.item(ck);

        // --- start solution: if there is another node this should be the PCDATA-node
        Node ns = child_child.getNextSibling();
        if (ns != null)
            child_child = ns;// w w w .  j av  a  2s.c o  m
        // --- end solution

        final short nt = child_child.getNodeType();

        // 1. look for a CDATA first...
        if (nt == Node.CDATA_SECTION_NODE) {
            if (trim)
                return ((String) child_child.getNodeValue()).trim();
            return (String) child_child.getNodeValue();
        }
    }

    for (int ck = 0; ck < list_child.getLength(); ck++) {
        Node child_child = (Node) list_child.item(ck);

        // --- start solution: if there is another node this should be the PCDATA-node
        Node ns = child_child.getNextSibling();
        if (ns != null)
            child_child = ns;
        // --- end solution

        final short nt = child_child.getNodeType();
        // 1. look for a CDATA first...
        if (nt == Node.TEXT_NODE) {
            if (trim)
                return ((String) child_child.getNodeValue()).trim();
            return (String) child_child.getNodeValue();
        }
    }

    return "";
}

From source file:Main.java

/**
 * Generates XPath expression with the option of the Node values appended
 *
 * @param node             the Node whose XPath is to be found
 * @param ignoreWhitespace the flag to indicate if Whitespace will be ignored
 * @param includeValues    the flag to indicate if Node values will be included
 * @param noIndex          the flag to indicate if Node indexes are included
 * @return the XPath string representation of the Node
 *///w  w w .  java 2s .c  o m

public static String generateXPath(Node node, boolean ignoreWhitespace, boolean includeValues, boolean noIndex)

{

    boolean noValues = !includeValues;

    if (node == null)

        return "";

    Node parent = node.getParentNode();

    int index = noIndex ? 0 : getXPathNodeIndex(node, ignoreWhitespace);

    String indexStr = "";

    if (index > 0)

        indexStr = "[" + Integer.toString(index) + "]";

    //printNode(node);

    //printNode(parent);

    if (node.getNodeType() == Node.DOCUMENT_NODE)

    {

        // return only the blank String, since all the other types are preceded with /

        return "";

    } else if (node.getNodeType() == Node.TEXT_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                (noValues ? node.getNodeValue() + indexStr : "/TEXT(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.ELEMENT_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                "/" + node.getNodeName() + indexStr;

    } else if (node.getNodeType() == Node.COMMENT_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                (noValues ? node.getNodeValue() + indexStr
                        : "/COMMENT(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.ENTITY_REFERENCE_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                (noValues ? node.getNodeValue() + indexStr
                        : "/EntityReference(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                (noValues ? node.getNodeValue() + indexStr : "/PI(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE)

    {

        return generateXPath(((Attr) node).getOwnerElement(), ignoreWhitespace, noValues, noIndex) +

                "/'@" + node.getNodeName() +

                (noValues ? "" : "=" + node.getNodeValue()) + "]";

    } else if (node.getNodeType() == Node.DOCUMENT_TYPE_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                (noValues ? node.getNodeValue() : "/DOCTYPE(" + node.getNodeName() + ")");

    } else if (node.getNodeType() == Node.CDATA_SECTION_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                (noValues ? node.getNodeValue() : "/CDATA(" + node.getNodeName() + ")");

    }

    // Wont reach this far but just in case

    return "";

}

From source file:com.qwazr.extractor.parser.ImageParser.java

private void browseNodes(String path, final Node root, final ParserFieldsBuilder result) {
    if (root == null)
        return;/*from w w w . j  a va  2  s.  c  o m*/
    switch (root.getNodeType()) {
    case Node.TEXT_NODE:
        result.add(ParserField.newString(path, null), root.getNodeValue());
        break;
    case Node.ELEMENT_NODE:
        final NamedNodeMap nnm = root.getAttributes();
        if (nnm != null)
            for (int i = 0; i < nnm.getLength(); i++)
                browseNodes(path, nnm.item(i), result);
        Node child = root.getFirstChild();
        while (child != null) {
            browseNodes(path + "/" + child.getNodeName(), child, result);
            child = child.getNextSibling();
        }
        break;
    case Node.ATTRIBUTE_NODE:
        path = path + "#" + root.getNodeName();
        result.add(ParserField.newString(path, null), root.getNodeValue());
        break;
    default:
        throw new NotImplementedException("Unknown attribute: " + root.getNodeType());
    }
}

From source file:com.example.fypv2.dataprocessor.OsmDataProcessor.java

@Override
public List<Marker> load(String rawData, int taskId, int colour) throws JSONException {
    Element root = convertToXmlDocument(rawData).getDocumentElement();

    List<Marker> markers = new ArrayList<Marker>();
    NodeList nodes = root.getElementsByTagName("node");

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        NamedNodeMap att = node.getAttributes();
        NodeList tags = node.getChildNodes();
        for (int j = 0; j < tags.getLength(); j++) {
            Node tag = tags.item(j);
            if (tag.getNodeType() != Node.TEXT_NODE) {
                String key = tag.getAttributes().getNamedItem("k").getNodeValue();
                if (key.equals("name")) {

                    String name = tag.getAttributes().getNamedItem("v").getNodeValue();
                    String id = att.getNamedItem("id").getNodeValue();
                    double lat = Double.valueOf(att.getNamedItem("lat").getNodeValue());
                    double lon = Double.valueOf(att.getNamedItem("lon").getNodeValue());

                    Log.v(MainFrame.TAG, "OSM Node: " + name + " lat " + lat + " lon " + lon + "\n");

                    Marker ma = new NavigationMarker(id, name, lat, lon, 0,
                            "http://www.openstreetmap.org/?node=" + id, taskId, colour);
                    markers.add(ma);/*ww w  .  ja  v a 2  s.c o  m*/

                    // skip to next node
                    continue;
                }
            }
        }
    }
    return markers;
}

From source file:TreeDumper2.java

private void dumpLoop(Node node, String indent) {
    switch (node.getNodeType()) {
    case Node.ATTRIBUTE_NODE:
        dumpAttributeNode((Attr) node, indent);
        break;/*from w  ww  .j av  a2 s  .  c o m*/
    case Node.CDATA_SECTION_NODE:
        dumpCDATASectionNode((CDATASection) node, indent);
        break;
    case Node.COMMENT_NODE:
        dumpCommentNode((Comment) node, indent);
        break;
    case Node.DOCUMENT_NODE:
        dumpDocument((Document) node, indent);
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        dumpDocumentFragment((DocumentFragment) node, indent);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        dumpDocumentType((DocumentType) node, indent);
        break;
    case Node.ELEMENT_NODE:
        dumpElement((Element) node, indent);
        break;
    case Node.ENTITY_NODE:
        dumpEntityNode((Entity) node, indent);
        break;
    case Node.ENTITY_REFERENCE_NODE:
        dumpEntityReferenceNode((EntityReference) node, indent);
        break;
    case Node.NOTATION_NODE:
        dumpNotationNode((Notation) node, indent);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        dumpProcessingInstructionNode((ProcessingInstruction) node, indent);
        break;
    case Node.TEXT_NODE:
        dumpTextNode((Text) node, indent);
        break;
    default:
        System.out.println(indent + "Unknown node");
        break;
    }

    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++)
        dumpLoop(list.item(i), indent + "   ");
}

From source file:DOMEdit.java

private static void outputloop(Node node, String indent) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        outputElement((Element) node, indent);
        break;// w  ww  .  jav a  2 s .  co m
    case Node.TEXT_NODE:
        outputText((Text) node, indent);
        break;
    case Node.CDATA_SECTION_NODE:
        outputCDATASection((CDATASection) node, indent);
        break;
    case Node.COMMENT_NODE:
        outputComment((Comment) node, indent);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        outputProcessingInstructionNode((ProcessingInstruction) node, indent);
        break;
    default:
        System.out.println("Unknown node type: " + node.getNodeType());
        break;
    }
}

From source file:com.nortal.jroad.util.SOAPUtil.java

/**
 * Returns whether given {@link Node} is text {@link Node}.
 *
 * @param node the {@link Node}/*w  w w  . ja  v a 2 s . c  o m*/
 * @return whether given node was text node
 */
public static boolean isTextNode(Node node) {
    return node != null ? Node.TEXT_NODE == node.getNodeType() : false;
}

From source file:DOMCopy.java

private static void outputloop(Node node, String indent) {
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            outputElement((Element) node, indent);
            break;
        case Node.TEXT_NODE:
            outputText((Text) node, indent);
            break;
        case Node.CDATA_SECTION_NODE:
            outputCDATASection((CDATASection) node, indent);
            break;
        case Node.COMMENT_NODE:
            outputComment((Comment) node, indent);
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            outputProcessingInstructionNode((ProcessingInstruction) node, indent);
            break;
        default:/*from w ww  .  j  a v  a2 s. c o m*/
            System.out.println("Unknown node type: " + node.getNodeType());
            break;
        }
    }

From source file:Main.java

protected static void print(PrintStream out, Node node) {
    if (node == null)
        return;/*w  w w. java 2s.c  o  m*/
    short type = node.getNodeType();
    switch (type) {
    case Node.DOCUMENT_NODE: {
        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        // out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
        NodeList nodelist = node.getChildNodes();
        int size = nodelist.getLength();
        for (int i = 0; i < size; i++)
            print(out, nodelist.item(i));
        break;
    }

    case Node.DOCUMENT_TYPE_NODE: {
        DocumentType docType = (DocumentType) node;
        out.print("<!DOCTYPE " + getDocumentTypeData(docType) + ">\n");
        break;
    }

    case Node.ELEMENT_NODE: {
        out.print('<');
        out.print(node.getNodeName());
        NamedNodeMap map = node.getAttributes();
        if (map != null) {
            int size = map.getLength();
            for (int i = 0; i < size; i++) {
                Attr attr = (Attr) map.item(i);
                out.print(' ');
                out.print(attr.getNodeName());
                out.print("=\"");
                out.print(normalize(attr.getNodeValue()));
                out.print('"');
            }
        }

        if (!node.hasChildNodes())
            out.print("/>");
        else {
            out.print('>');
            NodeList nodelist = node.getChildNodes();
            int numChildren = nodelist.getLength();
            for (int i = 0; i < numChildren; i++)
                print(out, nodelist.item(i));

            out.print("</");
            out.print(node.getNodeName());
            out.print('>');
        }
        break;
    }

    case Node.ENTITY_REFERENCE_NODE: {
        NodeList nodelist = node.getChildNodes();
        if (nodelist != null) {
            int size = nodelist.getLength();
            for (int i = 0; i < size; i++)
                print(out, nodelist.item(i));

        }
        break;
    }

    case Node.CDATA_SECTION_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    case Node.TEXT_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    case Node.PROCESSING_INSTRUCTION_NODE: {
        out.print("<?");
        out.print(node.getNodeName());
        String s = node.getNodeValue();
        if (s != null && s.length() > 0) {
            out.print(' ');
            out.print(s);
        }
        out.print("?>");
        break;
    }

    case Node.COMMENT_NODE: {
        out.print("<!--");
        out.print(node.getNodeValue());
        out.print("-->");
        break;
    }

    default: {
        out.print(normalize(node.getNodeValue()));
        break;
    }
    }
    out.flush();
}

From source file:Main.java

public static void getNodeData(Node node, StringBuffer buf) {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_FRAGMENT_NODE:
    case Node.DOCUMENT_NODE:
    case Node.ELEMENT_NODE: {
        for (Node child = node.getFirstChild(); null != child; child = child.getNextSibling()) {
            buf.append('<');
            buf.append(node.getNodeName());
            buf.append('>');
            getNodeData(child, buf);/*from   w w w  .ja  va  2 s  .  c  o  m*/
            buf.append("</");
            buf.append(node.getNodeName());
            buf.append('>');
        }
    }
        break;
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        buf.append(node.getNodeValue());
        break;
    case Node.ATTRIBUTE_NODE:
        buf.append(node.getNodeValue());
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        // warning(XPATHErrorResources.WG_PARSING_AND_PREPARING);
        break;
    default:
        // ignore
        break;
    }
}