Java XML Node Text Value getAppInfoText(final Node node)

Here you can find the source of getAppInfoText(final Node node)

Description

Given an AppInfo Node, recurse through all the children and build a Tag / value structure for all of the Element children.

License

Open Source License

Parameter

Parameter Description
node The node to look at.

Declaration

public static String getAppInfoText(final Node node) 

Method Source Code

//package com.java2s;
/*/*  w w  w. j  a v  a 2 s .  c  om*/
 * JBoss, Home of Professional Open Source.
 *
 * See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
 *
 * See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
 */

import org.w3c.dom.Node;

public class Main {
    /**
     * Given an AppInfo Node, recurse through all the children and build a Tag / value structure for all of the Element children.
     * 
     * @param node The node to look at.
     */
    public static String getAppInfoText(final Node node) {

        // is there anything to do?
        if (node == null) {
            return null;
        }

        // concatenate children text
        StringBuffer str = new StringBuffer();
        Node child = node.getFirstChild();
        str.append("<Application Information>"); //$NON-NLS-1$

        while (child != null) {
            short type = child.getNodeType();
            if (type == Node.ELEMENT_NODE) {
                str.append("\n\t<"); //$NON-NLS-1$
                str.append(child.getNodeName());
                str.append(">"); //$NON-NLS-1$
                str.append(getChildText(child));
                str.append("</"); //$NON-NLS-1$
                str.append(child.getNodeName());
                str.append(">"); //$NON-NLS-1$
            }
            child = child.getNextSibling();
        }

        for (int i = 0; i < node.getAttributes().getLength(); i++) {
            final Node next = node.getAttributes().item(i);
            str.append("\n\t<"); //$NON-NLS-1$
            str.append(next.getNodeName());
            str.append(">"); //$NON-NLS-1$
            str.append(getChildText(next));
            str.append("</"); //$NON-NLS-1$
            str.append(next.getNodeName());
        }

        str.append("\n</Application Information>"); //$NON-NLS-1$

        // return text value
        return str.toString();

    }

    /**
     * Returns the concatenated child text of the specified node. This method only looks at the immediate children of type
     * <code>Node.TEXT_NODE</code> or the children of any child node that is of type <code>Node.CDATA_SECTION_NODE</code> for the
     * concatenation. This method was copied from the org.apache.xerces.util.DOMUtil class.
     * 
     * @param node The node to look at.
     */
    public static String getChildText(final Node node) {

        // is there anything to do?
        if (node == null) {
            return null;
        }

        // concatenate children text
        StringBuffer str = new StringBuffer();
        Node child = node.getFirstChild();
        while (child != null) {
            short type = child.getNodeType();
            if (type == Node.TEXT_NODE) {
                str.append(child.getNodeValue());
            } else if (type == Node.CDATA_SECTION_NODE) {
                str.append(getChildText(child));
            }
            child = child.getNextSibling();
        }

        // return text value
        return str.toString();

    }
}

Related

  1. existNode(Node node, String nodeName, String textValue)
  2. extractNodeText(Node node)
  3. extractText(Node node)
  4. findNodeText(Node node)
  5. get_inner_text(Node node)
  6. getContentsOfTextOnlyNode(Node n)
  7. getContentText(Node n)
  8. getDescendentText(Node node, String name)
  9. getDirectText(org.w3c.dom.Element node)