Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

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

    public static Element getElementWithKeyFromDocument(final Document document, final String key) {
        final NodeList nodeList = document.getElementsByTagName(key);
        Element result = null;
        if (nodeList.getLength() > 0) {
            result = (Element) nodeList.item(0);
        }
        return result;
    }

    public static Element getElementWithKeyFromElement(final Element element, final String key) {
        final NodeList nodeList = element.getElementsByTagName(key);
        Element result = null;
        if (nodeList.getLength() > 0) {
            result = (Element) nodeList.item(0);
        }
        return result;
    }
}