2014-02-07 107 views
0

如果不需要,我只是不想重新發明輪子,所以我會問你以下問題,使用saxparser擴展將以下內容解析爲Java日期的最簡單方法是什麼?使用Java從XML解析日期

<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

您可以添加限制,只允許合理的值(例如1-12爲月份,1-31爲日期,+整數爲年份),但您無法處理閏年或年禾/ 31天。 – duffymo

+0

@PeterJaloveczki你到底在問什麼?通過「Java日期」你是指一個java.util.Date對象?如果month + day是可選的,那麼我不會看到你如何期望從一年內創建像j.u.Date這樣的日期時間對象。 –

回答

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); 
} 
+0

確保你打電話超級或以某種方式傳遞事件,否則在路上調試可能會很困難! – Randyaa

1

以下塊會給你YYYY或YYYY/MM或根據您的XML是什麼樣子YYYY/MM/DD的格式,它處理一些錯誤情況。然後,您可以轉向並將其格式化爲java.util.Date對象或任何您想要使用的對象。

在實施任何SAX過濾/處理時,請務必記住,您應該始終對任何攔截的事件調用「超級」,以確保您將事件傳遞給任何下游處理程序,否則它們將從流中消失,有人在某個時候會遇到一些麻煩。

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); 
}