【问题标题】:Parsing date from XML with Java使用 Java 从 XML 解析日期
【发布时间】:2014-02-07 10:09:22
【问题描述】:

如果没有必要,我只是不想重新发明轮子,所以我会问你以下问题,使用 saxparser 扩展将以下内容解析为 Java 日期的最简单方法是什么?

<start-date>
   <year>2012</year>
   <month>04</month>
   <day>10</day>
</start-date>


if (currentQName.equals("start-date")) {
         //return Java date the easiest way possible.
}

我的解决方案是保存所有三个而不是检查所有可能性,如果可能,我想避免这种情况。

棘手的是有来自 DTD 的限制,只有 YEAR 是强制性的,如果定义了月份,那么天也是强制性的。

谢谢!

【问题讨论】:

  • 您可以添加只允许合理值的限制(例如,1-12 代表月份,1-31 代表天,+ 整数代表年份),但您不能处理闰年或年份 w/ 31 天。
  • @PeterJaloveczki 你到底在问什么? “Java 日期”是指 java.util.Date 对象吗?如果月+日是可选的,那么我看不出您如何期望从一年中创建一个像 j.u.Date 这样的日期时间对象。

标签: java parsing date sax


【解决方案1】:
StringBuilder buf = new StringBuilder();
int year;
int month;
int day;

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    if (localName.equals("year")) {
        buf.setLength(0);
    } else if (localName.equals("month")) {
        buf.setLength(0);
    } else if (localName.equals("day")) {
        buf.setLength(0);
    }
}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {
    if (localName.equals("start-date")) {
       doSomethingWith(year,month,day);
    } else if (localName.equals("year")) {
        year = Integer.parseInt(buf.toString());
    } else if (localName.equals("month")) {
        month = Integer.parseInt(buf.toString());
    } else if (localName.equals("day")) {
        day = Integer.parseInt(buf.toString());
    }
}

@Override
public void characters(char chars[], int start, int length)
        throws SAXException {
    buf.append(chars, start, length);
}

【讨论】:

  • 确保调用 super 或以某种方式传递事件,否则后续调试可能会很困难!
【解决方案2】:

根据您的 XML 的外观,以下块将为您提供 YYYY 或 YYYY/MM 或 YYYY/MM/DD 的格式,并处理一些错误情况。然后,您可以转身并将其格式化为 java.util.Date 对象或任何您想使用它的对象。

请记住,在实现任何 SAX 过滤/处理时,您应该始终为拦截的任何事件调用“super”,以确保将事件传递给任何下游处理程序,否则它们将从流中消失在某些时候调试会遇到一些麻烦。

StringBuilder buf = null;

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    if ("month".equals(localName) || "day".equals(localName)) {
        if (buf != null) {
            buf.append("/");
        } else {
            throw new SAXException("something went wrong, we received a month and day outside a start-date");
        }
    } else if ("start-date".equals(localName)){
        //there's another error condition that should be handled here if we encounter a start-date but we're already buffering
        buf = new StringBuilder();
    }
    super.startElement(uri,localName,qName);
}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {
    if ("start-date".equals(localName)) {
        //buf will be int he format of YYYY OR YYYY/MM OR YYYY/MM/DD depending on what was in your XML.
        doSomethingWith(buf.toString());
    }
    super.endElement(uri,localName,qName); 
}

@Override
public void characters(char chars[], int start, int length)
        throws SAXException {
    if (buf != null) {
        buf.append(chars, start, length);
    }
    super.characters(chars,start,length);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    相关资源
    最近更新 更多