还是sax解析xml的问题?高手请看

snowring 2004-09-16 03:12:48
现在有个XML文件比较大,但我现在只需要取根节点下的属性的值,不想用dom,因为太大了,加载到内存中比较耗内存,但是我想取道要的属性值后,就退出程序,不要在往下在解析了,如何实现啊
public class DbXmlFile extends DefaultHandler{
    public void startElement(String uri, String localName, String tag,
Attributes attribs)
throws SAXParseException {

  System.out.println(localName);//这里把所有的element都打印出来了,
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(beginDate);
System.out.println(endDate);
//我想在这里就退出,让parser不要再解析下去了,因为我需要的值已经取到,而且下面非长大,可能比较耗时,但是这里打印每次都打出来,如何停止呢
}
}

public void parse(String xmlFileName) throws FileNotFoundException,    IOException,Exception
{
Reader in = new BufferedReader(new InputStreamReader(new FileInputStream(
new File(xmlFileName)), "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);
}
}

}
public static void main(String[] args) throws Exception
{
DbXmlFile dbXmlFile = new DbXmlFile();
dbXmlFile.parse("d:\\project\\xfk\\db\\date\\20040916134141601.xml");


XML的格式如下
<?xml version="1.0" encoding="GB2312"?>
<ROOT exportDate="2004/09/16 13:42:09.421 CST" beginDate = "2004-09-03"
endDate = "2004-09-21" >
  <TSJBLIST/>

  <CHECKRECORDLIST>
<CHECKRECORD num="1">
<ID>20040908154750426</ID>
<CHECKTIME>2004-09-07 00:00:00.0</CHECKTIME>
<CHECKER1ID>ddd</CHECKER1ID>
<CHECKER2ID>ddd</CHECKER2ID>
<UNITNAME>南京电视台</UNITNAME>
。。。。。。
,。,,。,,

</ROOT>
}
...全文
223 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xkak2 2004-09-20
  • 打赏
  • 举报
回复
为什么要用sax、dom呢?你只是要根的属性,自己写个xml的分析函数,xml的声明之后的第一个“<”“>”之间的难道不是根吗?
snowring 2004-09-17
  • 打赏
  • 举报
回复
程序没有异常,况且也执行到System.out.println("date=============="+fileBeginDate);
这边了,也打印出来了,但在最后System.out.println("beginDate--------------------------------"+ dbXmlFile.getFileBeginDate());///为什么这里打出的是NULL呢
在外面就打取不到啊,
江南愚子 2004-09-17
  • 打赏
  • 举报
回复
确定解析过程是在throw new SAXParseException("sss",new LocatorImpl());处退出的吗?没有这句能得到正确的结果吗?
snowring 2004-09-17
  • 打赏
  • 举报
回复
ding
snowring 2004-09-17
  • 打赏
  • 举报
回复
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,为什么啊

snowring 2004-09-17
  • 打赏
  • 举报
回复
xkak2(矗立云端) :
如果你只是想知道根的属性,何必用sax,自己写个函数,把根的属性读出来不就行了。
你是说用dom来解析吗,但xml很大时,不是要全部加载进去,这样不是很耗内存吗
pking2002 2004-09-17
  • 打赏
  • 举报
回复
你的fileEndDate到最后读出来的时候是null,我也遇到过这个问题,我是把变量变为static类型的才搞定,也没弄清楚是什么原因造成的
xkak2 2004-09-17
  • 打赏
  • 举报
回复
抛异常是个很不好的创意,你要知道抛出异常以后:程序的运行会比正常情况下慢2-3个数量级。
如果你只是想知道根的属性,何必用sax,自己写个函数,把根的属性读出来不就行了。
江南愚子 2004-09-16
  • 打赏
  • 举报
回复
你可以在想退出的地方抛出一个SAXException异常退出解析过程
snowring 2004-09-16
  • 打赏
  • 举报
回复
ding
snowring 2004-09-16
  • 打赏
  • 举报
回复
顶一下

62,622

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧