用Java解析XML中的数据

用Java解析XML中的数据,java,parsing,date,sax,Java,Parsing,Date,Sax,我只是不想在没有必要的情况下重新发明轮子,所以我将问您以下问题,使用saxparser扩展将以下内容解析为Java日期的最简单方法是什么 <start-date> <year>2012</year> <month>04</month> <day>10</day> </start-date> if (currentQName.equals("start-date")) {

我只是不想在没有必要的情况下重新发明轮子,所以我将问您以下问题,使用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.
}

2012
04
10
if(currentQName.equals(“开始日期”)){
//以最简单的方式返回Java日期。
}
我的解决方案是保存所有这三个选项,而不是检查所有的可能性,如果可能的话,我想避免这样做

棘手的是,有来自DTD的限制,只有年是强制性的,若定义了月,那个么天也是强制性的


谢谢

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

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);
}
请记住,在实现任何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);
}

您可以添加只允许合理值的限制(例如,月为1-12,日为1-31,年为+整数),但您不能处理闰年或年/31天。@PeterJaloveczki您到底在问什么?“Java日期”是指Java.util.date对象吗?如果月+日是可选的,那么我看不出如何从一年开始创建像j.u.date这样的日期时间对象。请确保调用super或以某种方式传递事件,否则调试过程可能会很困难!