还是sax解析xml的问题?高手请看 你可以在想退出的地方抛出一个SAXException异常退出解析过程 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class DbXmlFile extends DefaultHandler{ private Date fileCreateDate; private Date fileBeginDate; private Date fileEndDate; public String name; private XMLReader parser = new XMLReaderImpl(); public DbXmlFile(){ } public Date getFileCreateDate() { return fileCreateDate; } public void setFileCreateDate(Date fileCreateDate) { this.fileCreateDate = fileCreateDate; } public Date getFileBeginDate() { return fileBeginDate; } public void setFileBeginDate(Date fileBeginDate) { this.fileBeginDate = fileBeginDate; } public Date getFileEndDate() { return fileEndDate; } public void setFileEndDate(Date fileEndDate) { this.fileEndDate = fileEndDate; } public void startElement(String uri, String localName, String tag, Attributes attribs) throws SAXParseException { if (localName.equals("ROOT")) { String beginDate = attribs.getValue("beginDate"); String endDate = attribs.getValue("endDate"); this.fileBeginDate=Date.valueOf(beginDate ); this.fileEndDate=Date.valueOf(endDate); System.out.println("date=============="+fileBeginDate);//这里已经set过了,但最后取不到啊 //throw new SAXParseException("sss",new LocatorImpl()); } } public String parse(File xmlFile) throws FileNotFoundException, IOException,Exception { Reader in = new BufferedReader(new InputStreamReader(new FileInputStream(xmlFile), "UTF8")); try { parser.setContentHandler(new DbXmlFile()); parser.parse(new InputSource(in)); } catch (SAXException saxe) { if (saxe instanceof SAXParseException) { SAXParseException saxpe = (SAXParseException) saxe; int line = saxpe.getLineNumber(); int col = saxpe.getColumnNumber(); String publicID = saxpe.getPublicId(); String message = "XML parsing exception (" + publicID + ") line " + line + ":" + col; throw new Exception(message); } } return this.fileBeginDate.toString(); } public static void main(String[] args) throws Exception { DbXmlFile dbXmlFile = new DbXmlFile(); File file = new File("d:\\project\\xfk\\db\\date\\20040916134141601.xml"); System.out.println(dbXmlFile.parse(file)); System.out.println("beginDate--------------------------------"+ dbXmlFile.getFileBeginDate());///为什么这里打出的是NULL呢,在程序里面这个值已经设过了,怎么没取道啊 }}最后取出的beginDate都是null,为什么啊 确定解析过程是在throw new SAXParseException("sss",new LocatorImpl());处退出的吗?没有这句能得到正确的结果吗? 程序没有异常,况且也执行到System.out.println("date=============="+fileBeginDate);这边了,也打印出来了,但在最后System.out.println("beginDate--------------------------------"+ dbXmlFile.getFileBeginDate());///为什么这里打出的是NULL呢在外面就打取不到啊, 抛异常是个很不好的创意,你要知道抛出异常以后:程序的运行会比正常情况下慢2-3个数量级。如果你只是想知道根的属性,何必用sax,自己写个函数,把根的属性读出来不就行了。 你的fileEndDate到最后读出来的时候是null,我也遇到过这个问题,我是把变量变为static类型的才搞定,也没弄清楚是什么原因造成的 xkak2(矗立云端) : 如果你只是想知道根的属性,何必用sax,自己写个函数,把根的属性读出来不就行了。你是说用dom来解析吗,但xml很大时,不是要全部加载进去,这样不是很耗内存吗 为什么要用sax、dom呢?你只是要根的属性,自己写个xml的分析函数,xml的声明之后的第一个“<”“>”之间的难道不是根吗? 如何修改静态代码块 map的问题,其实还是编码的问题....... 关于List加入一个Map后的问题,大家帮帮忙啊 刚学数组,请问降序排列输出如何编写? 菜鸟求指点 怎么样在一个类中集中处理异常啊 JMenu如何关闭窗口? 有个问题请教 n.addYear()为什么结果显示year没加1呢? j2sdk-1_4_0_03-windows-i586.exe刚安装,下面的程序报错? 帮菜鸟一个小忙吧! 50分提问★关于applet用户界面的问题!swing高手进来看看! java小问题
private Date fileBeginDate;
private Date fileEndDate;
public String name;
private XMLReader parser = new XMLReaderImpl(); public DbXmlFile(){
} public Date getFileCreateDate() {
return fileCreateDate;
} public void setFileCreateDate(Date fileCreateDate) {
this.fileCreateDate = fileCreateDate;
} public Date getFileBeginDate() {
return fileBeginDate;
} public void setFileBeginDate(Date fileBeginDate) {
this.fileBeginDate = fileBeginDate;
} public Date getFileEndDate() {
return fileEndDate;
} public void setFileEndDate(Date fileEndDate) {
this.fileEndDate = fileEndDate;
} public void startElement(String uri, String localName, String tag,
Attributes attribs)
throws SAXParseException {
if (localName.equals("ROOT")) {
String beginDate = attribs.getValue("beginDate");
String endDate = attribs.getValue("endDate");
this.fileBeginDate=Date.valueOf(beginDate );
this.fileEndDate=Date.valueOf(endDate);
System.out.println("date=============="+fileBeginDate);//这里已经set过了,但最后取不到啊
//throw new SAXParseException("sss",new LocatorImpl());
}
} public String parse(File xmlFile) throws FileNotFoundException, IOException,Exception
{ Reader in = new BufferedReader(new InputStreamReader(new
FileInputStream(xmlFile), "UTF8"));
try {
parser.setContentHandler(new DbXmlFile());
parser.parse(new InputSource(in));
} catch (SAXException saxe) {
if (saxe instanceof SAXParseException) {
SAXParseException saxpe = (SAXParseException) saxe;
int line = saxpe.getLineNumber();
int col = saxpe.getColumnNumber();
String publicID = saxpe.getPublicId();
String message = "XML parsing exception (" + publicID +
") line " + line + ":" + col;
throw new Exception(message);
}
}
return this.fileBeginDate.toString();
} public static void main(String[] args) throws Exception
{
DbXmlFile dbXmlFile = new DbXmlFile();
File file = new File("d:\\project\\xfk\\db\\date\\20040916134141601.xml");
System.out.println(dbXmlFile.parse(file));
System.out.println("beginDate--------------------------------"+ dbXmlFile.getFileBeginDate());///为什么这里打出的是NULL呢,在程序里面这个值已经设过了,怎么没取道啊
}
}最后取出的beginDate都是null,为什么啊
这边了,也打印出来了,但在最后System.out.println("beginDate--------------------------------"+ dbXmlFile.getFileBeginDate());///为什么这里打出的是NULL呢
在外面就打取不到啊,
如果你只是想知道根的属性,何必用sax,自己写个函数,把根的属性读出来不就行了。
如果你只是想知道根的属性,何必用sax,自己写个函数,把根的属性读出来不就行了。
你是说用dom来解析吗,但xml很大时,不是要全部加载进去,这样不是很耗内存吗