SAX无法正确解析日期数据

09-12-27 xysniper

采用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.ContentHandler#characters(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.ContentHandler#endDocument()
	 */
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		System.out.println("XML文件解析结束"+i);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.ContentHandler#endElement(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.ContentHandler#endPrefixMapping(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.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
	 */
	public void setDocumentLocator(Locator locator) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
	 */
	public void skippedEntity(String name) throws SAXException {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.ContentHandler#startDocument()
	 */
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		System.out.println("XML文件开始解析");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.ContentHandler#startElement(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.ContentHandler#startPrefixMapping(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>

附件为XML文件

[该贴被xysniper于2009-12-27 16:56修改过]
attachment:


2009081502000071.xml