Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.*;

public class Main {
    /** Helper method - gets a named element's values as an array of <I>String</I>
    objects.
       @param pElement The parent <I>Element</I>.
       @param strName Name of the element.
       @return Array of <I>String</I> objects representing the values
      of the child elements.
     */
    public static String[] getElementStrings(Element pElement, String strName) {
        NodeList pNodeList = pElement.getElementsByTagName(strName);

        if (null == pNodeList)
            return null;

        int nLength = pNodeList.getLength();

        if (0 == nLength)
            return null;

        String[] strReturn = new String[nLength];

        for (int i = 0; i < nLength; i++)
            strReturn[i] = getNodeText(pNodeList.item(i));

        return strReturn;
    }

    /** Helper method - gets the text value of an element node. The text value
    is stored in the child nodes.
       @param pNode A org.w3c.dom.Node object.
       @return Element's text.
     */
    public static String getNodeText(Node pNode) {
        NodeList pChildren = pNode.getChildNodes();
        int nLength = pChildren.getLength();

        if (0 == nLength)
            return null;

        String strReturn = "";

        for (int i = 0; i < nLength; i++) {
            Node pChild = pChildren.item(i);

            if (pChild instanceof CharacterData)
                strReturn += ((CharacterData) pChild).getData();
        }

        if (0 == strReturn.length())
            return null;

        return strReturn;
    }
}