Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static String getCharacterDataFromObjectWithKey(final Object xmlObject, final String key) {
        String result = null;
        if (xmlObject instanceof Document) {
            result = getCharacterDataFromDocumentWithKey((Document) xmlObject, key);
        } else if (xmlObject instanceof Element) {
            result = getCharacterDataFromElementWithKey((Element) xmlObject, key);
        }
        return result;
    }

    public static String getCharacterDataFromDocumentWithKey(final Document document, final String key) {
        String result = null;
        if (document != null) {
            final NodeList jobNodeList = document.getElementsByTagName(key);
            final CharacterData characterData = (CharacterData) jobNodeList.item(0).getChildNodes().item(0);
            result = characterData.getNodeValue();

        }
        return result;
    }

    public static String getCharacterDataFromElementWithKey(final Element element, final String key) {
        String result = null;
        if (element != null) {
            final NodeList jobNodeList = element.getElementsByTagName(key);

            final Node node = jobNodeList.item(0);
            if (node != null) {
                final CharacterData characterData = (CharacterData) jobNodeList.item(0).getChildNodes().item(0);
                if (characterData != null) {
                    result = characterData.getNodeValue();
                }
            }
        }
        return result;
    }
}