当前位置: 代码迷 >> Java Web开发 >> SAX解析XML发生有关问题!
  详细解决方案

SAX解析XML发生有关问题!

热度:81   发布时间:2016-04-17 14:04:27.0
SAX解析XML发生问题!急!急!急!
解析日期的字符串小部分发生丢失,导致我导入数据库出错,代码和XML文件样本如下,读出的日期一小部分会变成 "-04-28   10:58:00.0 ",完整的应该是 "2007-04-28   10:58:00.0 ",
public   class   SAXReader   extends   DefaultHandler{

dbcon   connect   =   new   dbcon();
String   columnValue   =   null;
String   tableName   =   null;
String   columnName   =   null;
String   sql   =     "insert   into   "   ;

public   void   startDocument()   throws   SAXException   {
}
public   void   processingInstruction(String   taget,   String   data)   throws   SAXException   {

}

public   void   startElement(String   uri,   String   localName,   String   qName,   Attributes   attr)   throws   SAXException   {

if( "TABLE ".equals(qName))
{
for(int   i   =   0;i <attr.getLength();i++)
{
tableName   =   attr.getValue(i);
}
sql   +=   tableName   +   "   values( ";
}
else
{
columnName   =   qName;
}


}
public   void   characters(char[]   ch,   int   start,   int   length)   throws   SAXException   {
columnValue=new   String(ch,start,length).trim();
if( " ".equals(columnValue))
{
columnValue   =   " ' ' ";
}
else  
{
if(! "null ".equals(columnValue)&&! "NULL ".equals(columnValue)&&columnValue!=null&&! " ".equals(columnValue))
{
columnValue   =   " ' "+   columnValue+ " ' ";
}
}
//System.out.println(columnValue);
}
public   void   endElement(String   uri,   String   localName,   String   qName)   throws   SAXException   {
if(! "DATA ".equals(qName)&&! "TABLE ".equals(qName))
{
if(! "LASTUPDATE ".equals(columnName))
{
sql   +=     columnValue   + ", ";  
}
if( "LASTUPDATE ".equals(columnName))
{
sql   +=     columnValue;  
}
}

if( "TABLE ".equals(qName))
{
sql   +=   ") ";
System.out.println(sql);
insert(sql);
sql   =   "insert   into   ";
columnName   =   null;
columnValue   =   null;
}
}
public   void   endDocument()   throws   SAXException   {

}

public   int   insert(String   sql)
{
int   i   =   0;
if(connect!=null)
{
i   =   connect.executeUpdate(sql);
}
return   i   ;
}

XML文件样本:
<DATA>
    <TABLE   tableName= "user02 ">
        <USERID> 1000000001 </USERID>
        <BIRTHDAY> null </BIRTHDAY>
        <NATIVE> null </NATIVE>
        <BIRTHPLACE> null </BIRTHPLACE>
        <MARRIED> null </MARRIED>
  相关解决方案