SAX 解析器和来自网络的文件 [英] SAX parser and a file from the nework

查看:67
本文介绍了SAX 解析器和来自网络的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好开发人员...只是为了确定,我想问这个问题:

Hello fellow developers... just to make sure, I want to ask this question:

XML SAX 解析器如何访问它正在解析的 .xml 文件?它是否从给定的 URL 下载整个文件?

How does XML SAX parser access the .xml file it is parsing? Does it download the whole file from the given URL?

打破解析是否有任何用处,以便我们可以节省一些千字节的数据?

Is there any use in breaking the parsing so that we can save some kilobytes of data?

想象一个包含有序项目的大型 .xml 文件.我们只需要顶部的几个项目,其他项目可能已经被处理和存储.当我在特定点停止解析时,我是否会保存一些数据(当然我会节省一些时间).

Imagine a large .xml file with ordered items. We need only several items from the top, the other items may already be processed and stored. When I stop the parsing at specific point, will I save some data (surely I will save some time).

感谢您的回答.

推荐答案

SAX 解析器实现存在于许多语言中,答案可能是特定于实现的.但至少常见的 Java 实现可以从流中读取 xml,而无需下载整个内容.

SAX parser implementations exist in many languages, and the answer may be implementation-specific. But at least the common Java implementations can read the xml from a stream and needn't download the entire thing.

调用 Java SAX 解析器从 URL 解析通常看起来像

An invocation of a Java SAX parser to parse from a URL usually looks something like

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        MyHandler handler = new MyHandler();
        xr.setContentHandler(handler);
        xr.parse(new InputSource(sourceUrl.openStream()));

其中处理程序 MyHandler 是您定义的实现 org.xml.sax.ContentHandler 的类(最容易通过扩展 org.xml.sax.helpers.DefaultHandler) 和 sourceURL 是 URL 的 java.net.URL.

where the handler MyHandler is a class you define implementing org.xml.sax.ContentHandler (most easily by extending org.xml.sax.helpers.DefaultHandler) and sourceURL is a java.net.URL for the URL.

当然,所有这些都必须包含在 try-catch 中...

Of course all this has to be enclosed in a try-catch...

您的处理程序可以抛出一个异常,表明它已到达您要解析的内容的末尾,并且通过捕获此异常,您的程序可以干净地完成而无需读取整个流.

Your handler can throw an exception signaling that it has reached the end of what you want to parse, and by catching this exception, your program can cleanly finish without reading the entire stream.

这篇关于SAX 解析器和来自网络的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆