This will look at the children of the XML element that have a tag name passed. - Java XML

Java examples for XML:XML Element Child

Description

This will look at the children of the XML element that have a tag name passed.

Demo Code


//package com.java2s;

import org.w3c.dom.*;

public class Main {
    /**//from w w w .j  av a  2 s  .co  m
     * This will look at the children of the element that have a
     * tag name passed.  It will then look at the child text nodes
     * and return a trim'ed version of the first Text node as the
     * text of the tag. For example given the XML source:
     *
     *    <person>
     *       Bill E. Bob
     *    </person>
     *
     *
     *    "Bill E. Bob" = textForFirstTag(element, "person");
     *
     * @return java.lang.String
     * @param element org.w3c.dom.Element
     * @param tag java.lang.String
     */
    public static String textForFirstTag(Element element, String tag) {
        if (!element.hasChildNodes())
            return ""; // Should never happen if we are passed a valid Element node

        NodeList tagList = element.getChildNodes();
        int tagCount = tagList.getLength();
        if (0 == tagCount) // No tags
            return "";

        for (int j = 0; j < tagCount; ++j) {
            Node tagNode = tagList.item(j);
            if (isMatchingNode(tagNode, tag)) {

                // Look at the children of this tag element.  There should be
                // one or more Text nodes.

                NodeList list = tagNode.getChildNodes();
                int count = list.getLength();
                for (int i = 0; i < count; ++i) {
                    Node node = list.item(i);
                    if (node instanceof Text) {
                        String value = trim((Text) node);
                        if (!("".equals(value)))
                            return value; // Return the trim()'d text of the first child text node
                    }
                } // for i...
            } // if match
        } // for j...
        return "";
    }

    /**
     * This is a utility method that will returns a boolean
     * indicating of the node is an Element and the name
     * matches the tag passed
     *
     * @return boolean
     * @param node org.w3c.dom.Node
     * @param tag java.lang.String
     */
    private static boolean isMatchingNode(Node node, String tag) {
        if (node instanceof Element)
            if (node.getNodeName().equals(tag))
                return true;
        return false;
    }

    /**
     * This will trim trailing and leading white space from a Text
     * node in a DOM structure.
     *
     * @return java.lang.String
     * @param textNode org.w3c.dom.Text
     */
    public static String trim(Text textNode) {
        String text = textNode.getNodeValue();
        if (null == text)
            return "";

        char[] array = text.toCharArray();
        int length = array.length;
        if (0 == length)
            return "";

        // Strip leading white space

        int start = 0;
        boolean isWhite = Character.isWhitespace(array[start]);
        while (isWhite) {
            ++start;
            if (start >= length)
                return ""; // It was all white
            isWhite = Character.isWhitespace(array[start]);
        }

        // Strip trailing white space

        int end = length - 1;
        isWhite = Character.isWhitespace(array[end]);
        while (isWhite) {
            --end;
            if (end <= 0)
                return ""; // It was all white
            isWhite = Character.isWhitespace(array[end]);
        }

        // If we get here, we had some non-whitespace

        return new String(array, start, end - start + 1);
    }
}

Related Tutorials