Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    /**
     * Loads an optional datetime element value from a XML element
     */
    public static DateTime getDateTimeNode(Element element, String name) {
        Element child = getSingleElement(element, name);

        if (child == null)
            return null;

        String value = child.getTextContent();

        if (value == null)
            return null;

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

        value = value.replace('T', ' ').replace("Z", "");

        DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        return dtf.parseDateTime(value);
    }

    /**
     * Returns a single element having a given tag
     */
    public static Element getSingleElement(Element element, String tagName) {
        Node node = element.getFirstChild();

        while (node != null) {
            if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().compareTo(tagName) == 0)
                return (Element) node;

            node = node.getNextSibling();
        }

        return null;
    }
}