SAX無法正確解析日期資料

xysniper發表於2009-12-27
採用Xerces2.4解析XML時,發現對於日期型資料解析出錯,如updatedate為2007-12-12T16:00:00.000Z,只解析出16:00:00.000Z,前面2007-12-12T丟掉了,如果把出錯記錄單獨拿出來解析,卻沒有問題,請問是怎麼回事呢?

  程式清單
  /**
 * 
 */
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修改過]

相關文章