SAX無法正確解析日期資料
採用Xerces2.4解析XML時,發現對於日期型資料解析出錯,如updatedate為2007-12-12T16:00:00.000Z,只解析出16:00:00.000Z,前面2007-12-12T丟掉了,如果把出錯記錄單獨拿出來解析,卻沒有問題,請問是怎麼回事呢?
附件為XML檔案
程式清單 /** * */ package com.java.application.xml; import java.io.IOException; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.XMLReaderFactory; public class SAXParser { public static void main(String[] args) throws Exception { SAXParser sax = new SAXParser(); sax.parseXMLFile("C:/2009081502000071.xml"); // sax.parseXMLFile("C:/t2.xml"); // BASE64Encoder encoder = new BASE64Encoder(); // encoder.encodeBuffer(new FileInputStream(new File("c:/2009081502000071.xml")), new FileOutputStream(new File("c:/2009081502000071out.xml"))); } // 解析文件 private void parseXMLFile(String fileName) { try { XMLReader parser = XMLReaderFactory .createXMLReader("org.apache.xerces.parsers.SAXParser"); parser.setContentHandler(new XMLContentHandler()); // 開始解析文件 long startTime = System.currentTimeMillis(); parser.parse(fileName); long endTime = System.currentTimeMillis(); System.out.println("Used time: " + (endTime - startTime)); } catch (IOException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } } } 內容管理器: /** * */ package com.java.application.xml; import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; import org.xml.sax.Locator; import org.xml.sax.SAXException; public class XMLContentHandler implements ContentHandler { // DTD中定義的元素名 private static final String ELEMENT_PROVINCEID = "provinceId"; private static final String ELEMENT_VERSION = "version"; private static final String ELEMENT_MSISDN = "msisdn"; private static final String ELEMENT_SEQUENCEID = "sequenceId"; private static final String ELEMENT_IMSI = "imsi"; private static final String ELEMENT_USERTYPE = "userType"; private static final String ELELENT_USERSTAT = "userStat"; private static final String ELELENT_USERBRAND = "userbrand"; private static final String ELELENT_USERSCPTYPE = "userSCPType"; private static final String ELELENT_USERPREPAIDID = "userPrepaidID"; private static final String ELELENT_USERSERVICETYPE = "userServiceType"; private static final String ELELENT_BACKUP = "backup"; private static final String ELELENT_UPDATEDATE = "updateDate"; private static final String ELEMENT_USERSTATINFORECORD = "userstateInfoRecord"; // private static final String ELEMENT_LINE = "breakLine"; // 當前元素的資料 private String currentData = ""; private int i = 1; /* * (non-Javadoc) * * @see org.xml.sax.ContentHandlercharacters(char[], int, int) */ public void characters(char[] ch, int start, int length) throws SAXException { currentData = String.valueOf(ch, start, length).trim(); // System.out.println("XML解析器成功解析到元素資料"); } /* * (non-Javadoc) * * @see org.xml.sax.ContentHandlerendDocument() */ public void endDocument() throws SAXException { // TODO Auto-generated method stub System.out.println("XML檔案解析結束"+i); } /* * (non-Javadoc) * * @see org.xml.sax.ContentHandlerendElement(java.lang.String, * java.lang.String, java.lang.String) */ public void endElement(String uri, String localName, String name) throws SAXException { // TODO Auto-generated method stub if (localName.equalsIgnoreCase(ELEMENT_PROVINCEID)) { System.out.println(localName + ":" + currentData); } if (localName.equalsIgnoreCase(ELEMENT_VERSION)) { System.out.println(localName + ":" + currentData); } if (localName.equalsIgnoreCase(ELEMENT_MSISDN)) { System.out.println(localName + ":" + currentData); } if (localName.equalsIgnoreCase(ELEMENT_IMSI)) { System.out.println(localName + ":" + currentData); } if (localName.equalsIgnoreCase(ELEMENT_SEQUENCEID)) { System.out.println(localName + ":" + currentData); } if (localName.equalsIgnoreCase(ELEMENT_USERTYPE)) { System.out.println(localName + ":" + currentData); } if (localName.equalsIgnoreCase(ELELENT_USERSTAT)) { System.out.println(localName + ":" + currentData); } if (localName.equalsIgnoreCase(ELELENT_USERBRAND)) { System.out.println(localName + ":" + currentData); } if (localName.equalsIgnoreCase(ELELENT_USERSCPTYPE)) { System.out.println(localName + ":" + currentData); } if (localName.equalsIgnoreCase(ELELENT_USERPREPAIDID)) { System.out.println(localName + ":" + currentData); } if (localName.equalsIgnoreCase(ELELENT_USERSERVICETYPE)) { System.out.println(localName + ":" + currentData); } if (localName.equalsIgnoreCase(ELELENT_BACKUP)) { System.out.println(localName + ":" + currentData); } if (localName.equalsIgnoreCase(ELELENT_UPDATEDATE)) { if (currentData.length() < 24) { System.out.println(localName + ":" + currentData); System.exit(0); i++; System.out.println("====>"+i); } System.out.println(localName + ":" + currentData); } if (localName.equalsIgnoreCase(ELEMENT_USERSTATINFORECORD)) { System.out.println("------end--------"); currentData = null; } // System.out.println("XML解析器解析元素" + localName + "結束"); } /* * (non-Javadoc) * * @see org.xml.sax.ContentHandlerendPrefixMapping(java.lang.String) */ public void endPrefixMapping(String prefix) throws SAXException { // TODO Auto-generated method stub // System.out.println("XML解析器解析名稱空間結束"); } /* * (non-Javadoc) * * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int) */ public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub System.out.println("ignorableWhitespace: "); } /* * (non-Javadoc) * * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, * java.lang.String) */ public void processingInstruction(String target, String data) throws SAXException { // TODO Auto-generated method stub } /* * (non-Javadoc) * * @see org.xml.sax.ContentHandlersetDocumentLocator(org.xml.sax.Locator) */ public void setDocumentLocator(Locator locator) { // TODO Auto-generated method stub } /* * (non-Javadoc) * * @see org.xml.sax.ContentHandlerskippedEntity(java.lang.String) */ public void skippedEntity(String name) throws SAXException { // TODO Auto-generated method stub } /* * (non-Javadoc) * * @see org.xml.sax.ContentHandlerstartDocument() */ public void startDocument() throws SAXException { // TODO Auto-generated method stub System.out.println("XML檔案開始解析"); } /* * (non-Javadoc) * * @see org.xml.sax.ContentHandlerstartElement(java.lang.String, * java.lang.String, java.lang.String, org.xml.sax.Attributes) */ public void startElement(String uri, String localName, String name, Attributes atts) throws SAXException { // TODO Auto-generated method stub // System.out.println("XML解析器開始解析元素" + localName); if (localName.equalsIgnoreCase(ELEMENT_USERSTATINFORECORD)) { System.out.println("-------start-------"+i); } } /* * (non-Javadoc) * * @see org.xml.sax.ContentHandlerstartPrefixMapping(java.lang.String, * java.lang.String) */ public void startPrefixMapping(String prefix, String uri) throws SAXException { // TODO Auto-generated method stub // System.out.println("XML解析器開始解析名稱空間"); } } <p class="indent"> |
附件為XML檔案
[該貼被xysniper於2009-12-27 16:56修改過]
相關文章
- JAVA-使用SAX解析XML資料JavaXML
- 日期的正確儲存方式
- 為什麼getYear()無法取得正確年份
- DOM解析和SAX解析
- Java 生成的 RSA 公鑰在 PHP 無法正確解析問題解決記錄JavaPHP
- 快速修復應用程式無法正確啟動
- Oracle打補丁時無法正確顯示英文Oracle
- sax解析例子演示
- 海關資料如何正確使用
- 正確停止資料泵程式
- 當希特勒被告知他的程式無法永久正確時
- jaxp的sax解析操作
- java使用sax解析xmlJavaXML
- python中無法正確讀取.mat檔案的解決辦法Python
- dom解析和sax解析的區別
- 使用JAXP進行DOM解析_SAX解析
- 談如何正確理解 IP 資料的覆蓋率,兼談正確率~
- XML解析-最快的方式SAXXML
- python使用SAX解析xmlPythonXML
- 資料混亂如何正確使用CRM
- Nginx+Tomcat無法正確獲取的域名和ip地址NginxTomcat
- 使DevExpress日期控制元件正確顯示 (轉)devExpress控制元件
- 單例模式的正確寫法單例模式
- DLP資料防洩漏之正確概念
- 【PM&資料】如何正確地利用產品資料【上】
- Laravel + dingoapi + jwt 使用者認證無法正確指定 guard 的解決辦法LaravelGoAPIJWT
- Sax 解析xml檔案時, 資料資訊不完整問題解決方法XML
- 為什麼我在2000和resin下無法正確安裝?
- git(osx)上的一個git commit無法正確提交的問題GitMIT
- Fragment全解析(2):正確的使用姿勢Fragment
- KMB:正確使用媒體和資料調查
- 資料庫無法update資料庫
- Android系列--DOM、SAX、Pull解析XMLAndroidXML
- python 解析xml 檔案: SAX方式PythonXML
- Python解析xml大檔案(sax)PythonXML
- POI 使用SAX讀取大資料Excel大資料Excel
- Python爬取免費IP代理時,無法解析到資料Python
- 解決drf_yasg中的SwaggerAPI無法正確分組問題SwaggerAPI