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

/**
 * Get textvalue from element. Loop all #text chidlnoes.
 * This is used by getText() methods, endusers usually
 * should not call this directly./*  w w  w.  java 2s .  co  m*/
 */
private static String getSimpleText(Element element) {
    if (element == null)
        return null;
    StringBuilder sb = new StringBuilder();
    NodeList nodes = element.getChildNodes();
    Node node;
    for (int i = 0; i < nodes.getLength(); i++) {
        node = nodes.item(i);
        if (node.getNodeType() == Node.TEXT_NODE)
            sb.append(node.getNodeValue());
    }
    return sb.toString().trim();
}

From source file:eu.annocultor.xconverter.impl.XmlElementForVelocity.java

public String getValue() {
    //      if (element.getFirstChild() != null && element.getFirstChild().getNodeType() == Node.CDATA_SECTION_NODE)
    //      {//w  w w .  j a  v  a2s  . c o m
    //         System.out.println("hi");
    //      }
    if (element.getFirstChild() != null && (element.getFirstChild().getNodeType() == Node.TEXT_NODE
            || element.getFirstChild().getNodeType() == Node.CDATA_SECTION_NODE)) {
        String value = element.getFirstChild().getNodeValue();
        if (value != null)
            value = value.trim();
        return value;
    }
    return null;
}

From source file:Main.java

/**
 * Print a Node tree recursively.//  w w w  . j a  v  a  2 s  .  c om
 * @param node A DOM tree Node
 * @return An xml String representation of the DOM tree.
 */
public static String print(Node node) {
    if (node == null) {
        return null;
    }

    StringBuffer xml = new StringBuffer(100);
    int type = node.getNodeType();

    switch (type) {
    // print element with attributes
    case Node.ELEMENT_NODE: {
        xml.append('<');
        xml.append(node.getNodeName());

        NamedNodeMap attrs = node.getAttributes();
        int length = attrs.getLength();
        ;

        for (int i = 0; i < length; i++) {
            Attr attr = (Attr) attrs.item(i);
            xml.append(' ');
            xml.append(attr.getNodeName());
            xml.append("=\"");

            //xml.append(normalize(attr.getNodeValue()));
            xml.append(attr.getNodeValue());
            xml.append('"');
        }

        xml.append('>');

        NodeList children = node.getChildNodes();

        if (children != null) {
            int len = children.getLength();

            for (int i = 0; i < len; i++) {
                xml.append(print(children.item(i)));
            }
        }

        break;
    }

    // handle entity reference nodes
    case Node.ENTITY_REFERENCE_NODE: {
        NodeList children = node.getChildNodes();

        if (children != null) {
            int len = children.getLength();

            for (int i = 0; i < len; i++) {
                xml.append(print(children.item(i)));
            }
        }

        break;
    }

    // print cdata sections
    case Node.CDATA_SECTION_NODE: {
        xml.append("<![CDATA[");
        xml.append(node.getNodeValue());
        xml.append("]]>");

        break;
    }

    // print text
    case Node.TEXT_NODE: {
        //xml.append(normalize(node.getNodeValue()));
        xml.append(node.getNodeValue());

        break;
    }

    // print processing instruction
    case Node.PROCESSING_INSTRUCTION_NODE: {
        xml.append("<?");
        xml.append(node.getNodeName());

        String data = node.getNodeValue();

        if ((data != null) && (data.length() > 0)) {
            xml.append(' ');
            xml.append(data);
        }

        xml.append("?>");

        break;
    }
    }

    if (type == Node.ELEMENT_NODE) {
        xml.append("</");
        xml.append(node.getNodeName());
        xml.append('>');
    }

    return xml.toString();
}

From source file:Main.java

/**
 * Serializes the given XML tree to string form, including the standard
 * XML header and indentation if desired. This method relies on the
 * serialization API from Apache Xerces, since JAXP has on equivalent.
 * //  ww w  . j a v  a2s.  co  m
 * @param xml
 *            The XML tree to serialize.
 * @param printHeader
 *            True if you want the XML header printed before the XML.
 * @param printIndents
 *            True if you want pretty-printing - child elements will be
 *            indented with symmetry.
 * @return The string representation of the given Node.
 */
public static String toString(final Node xml, final boolean printHeader, final boolean printIndents) {
    final short type = xml.getNodeType();

    if (type == Node.TEXT_NODE)
        return xml.getNodeValue();

    //
    // NOTE: This serialization code is not part of JAXP/DOM - it is 
    //       specific to Xerces and creates a Xerces dependency for 
    //       this class.
    //
    final XMLSerializer serializer = new XMLSerializer();
    serializer.setNamespaces(true);

    final OutputFormat formatter = new OutputFormat();
    formatter.setOmitXMLDeclaration(!printHeader);
    formatter.setIndenting(printIndents);
    serializer.setOutputFormat(formatter);

    final StringWriter writer = new StringWriter();
    serializer.setOutputCharStream(writer);

    try {
        if (type == Node.DOCUMENT_NODE)
            serializer.serialize((Document) xml);
        else
            serializer.serialize((Element) xml);
    }

    //
    // we are using a StringWriter, so this "should never happen". the 
    // StringWriter implementation writes to a StringBuffer, so there's 
    // no file I/O that could fail.
    //
    // if it DOES fail, we re-throw with a more serious error, because 
    // this a very common operation.
    //
    catch (final IOException error) {
        throw new RuntimeException(error.getMessage(), error);
    }

    return writer.toString();
}

From source file:MainClass.java

private void dumpLoop(Node node, String indent) {
    switch (node.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
        System.out.println(indent + "CDATA_SECTION_NODE");
        break;//ww  w.  j a v  a 2s .  c  om
    case Node.COMMENT_NODE:
        System.out.println(indent + "COMMENT_NODE");
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
        break;
    case Node.DOCUMENT_NODE:
        System.out.println(indent + "DOCUMENT_NODE");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        System.out.println(indent + "DOCUMENT_TYPE_NODE");
        break;
    case Node.ELEMENT_NODE:
        System.out.println(indent + "ELEMENT_NODE");
        break;
    case Node.ENTITY_NODE:
        System.out.println(indent + "ENTITY_NODE");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.println(indent + "ENTITY_REFERENCE_NODE");
        break;
    case Node.NOTATION_NODE:
        System.out.println(indent + "NOTATION_NODE");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
        break;
    case Node.TEXT_NODE:
        System.out.println(indent + "TEXT_NODE");
        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:com.jereksel.rommanager.XMLParser.java

/**
 * Getting node value/* w  ww.  ja v a  2  s .  c o  m*/
 *
 * @param elem element
 */
public final String getElementValue(Node elem) {
    Node child;
    if (elem != null) {
        if (elem.hasChildNodes()) {
            for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
                if (child.getNodeType() == Node.TEXT_NODE) {
                    return child.getNodeValue();
                }
            }
        }
    }
    return "";
}

From source file:com.ruzhi.demo.lifeserverweb.StringUtil.java

/**
 * html//from   ww w  . j  a  va 2  s . c o m
 * @return
 */
/*public static String filterHtmlTag(String html){
try {
StringBuffer sb=new StringBuffer();
DOMFragmentParser parser = new DOMFragmentParser();
 HTMLDocument document = new HTMLDocumentImpl();
 DocumentFragment fragment = document.createDocumentFragment();
 InputSource is = new InputSource(new StringReader(html));
 is.setEncoding("GBK");
   parser.parse(is, fragment);
 getText(sb,fragment);
 return sb.toString();
  } catch (SAXException e) {
  } catch (IOException e) {
  }
  return null;
}*/
private static void getText(StringBuffer sb, Node node) {
    if (node.getNodeType() == Node.TEXT_NODE) {
        sb.append(node.getNodeValue());//????
    }
    NodeList children = node.getChildNodes();
    if (children != null) {
        int len = children.getLength();
        for (int i = 0; i < len; i++) {
            getText(sb, children.item(i));//??DOM
        }
    }
}

From source file:Main.java

/**
 * Performs the actual recursive dumping of a DOM tree to a given
 * <CODE>PrintStream</CODE>. Note that dump is intended to be a detailed
 * debugging aid rather than pretty to look at. 
 * /*from  w w  w  .j a  va2  s.c  o  m*/
 * @param    out            The <CODE>PrintStream</CODE> to write to.
 * @param    node         The <CODE>Node</CODE> under consideration.
 * @param    indent         The level of indentation.
 * @see      #dump(Node)
 * @see      #dump(PrintStream, Node)
 * @since   TFP 1.0
 */
private static void doDump(PrintStream out, final Node node, int indent) {
    if (node != null) {
        for (int index = 0; index < indent; ++index)
            out.write(' ');

        switch (node.getNodeType()) {
        case Node.DOCUMENT_NODE: {
            Document document = (Document) node;

            out.println("DOCUMENT:");

            doDump(out, document.getDoctype(), indent + 1);
            doDump(out, document.getDocumentElement(), indent + 1);
            break;
        }

        case Node.DOCUMENT_TYPE_NODE: {
            DocumentType type = (DocumentType) node;

            out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId="
                    + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]");
            break;
        }

        case Node.ELEMENT_NODE: {
            Element element = (Element) node;

            out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name="
                    + format(element.getLocalName()) + "]");

            NamedNodeMap attrs = element.getAttributes();

            for (int index = 0; index < attrs.getLength(); ++index)
                doDump(out, attrs.item(index), indent + 1);

            for (Node child = element.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }
        case Node.ATTRIBUTE_NODE: {
            Attr attr = (Attr) node;

            out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix="
                    + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value="
                    + format(attr.getNodeValue()) + "]");
            break;
        }

        case Node.TEXT_NODE: {
            Text text = (Text) node;

            out.println("TEXT: [" + format(text.getNodeValue()) + "]");

            for (Node child = text.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }

        case Node.CDATA_SECTION_NODE: {
            CDATASection data = (CDATASection) node;

            out.println("CDATA: [" + format(data.getNodeValue()) + "]");
            break;
        }

        case Node.COMMENT_NODE: {
            Comment comm = (Comment) node;

            out.println("COMMENT: [" + format(comm.getNodeValue()) + "]");
            break;
        }

        default:
            out.println("UNKNOWN: [type=" + node.getNodeType() + "]");
            break;
        }
    }
}

From source file:DOMDump.java

private void dumpLoop(Node node, String indent) {
    switch (node.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
        System.out.println(indent + "CDATA_SECTION_NODE");
        break;//w w  w. jav a 2  s . co m
    case Node.COMMENT_NODE:
        System.out.println(indent + "COMMENT_NODE");
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
        break;
    case Node.DOCUMENT_NODE:
        System.out.println(indent + "DOCUMENT_NODE");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        System.out.println(indent + "DOCUMENT_TYPE_NODE");
        break;
    case Node.ELEMENT_NODE:
        System.out.println(indent + "ELEMENT_NODE");
        break;
    case Node.ENTITY_NODE:
        System.out.println(indent + "ENTITY_NODE");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.println(indent + "ENTITY_REFERENCE_NODE");
        break;
    case Node.NOTATION_NODE:
        System.out.println(indent + "NOTATION_NODE");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
        break;
    case Node.TEXT_NODE:
        System.out.println(indent + "TEXT_NODE");
        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:Main.java

public final static Class<? extends Node> toClass(final short nodeType) {
    switch (nodeType) {
    case Node.ATTRIBUTE_NODE:
        return Attr.class;
    case Node.CDATA_SECTION_NODE:
        return CDATASection.class;
    case Node.COMMENT_NODE:
        return Comment.class;
    case Node.DOCUMENT_FRAGMENT_NODE:
        return DocumentFragment.class;
    case Node.DOCUMENT_NODE:
        return Document.class;
    case Node.DOCUMENT_TYPE_NODE:
        return DocumentType.class;
    case Node.ELEMENT_NODE:
        return Element.class;
    case Node.ENTITY_NODE:
        return Entity.class;
    case Node.ENTITY_REFERENCE_NODE:
        return EntityReference.class;
    case Node.NOTATION_NODE:
        return Notation.class;
    case Node.PROCESSING_INSTRUCTION_NODE:
        return ProcessingInstruction.class;
    case Node.TEXT_NODE:
        return Text.class;
    }//w ww .  j  a  va  2s .  co m
    throw new RuntimeException("Unrecognized node type " + nodeType);
}