快速,轻量级的XML解析器 [英] Fast, lightweight XML parser

查看:190
本文介绍了快速,轻量级的XML解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个特定格式的XML文档,我将被推送。这个文件永远是一样的,所以它是非常严格的。



我需要解析这个,以便我可以把它转换成JSON(好吧,一个稍微混合版本,所以有人否则可以使用它与DOJO)。我的问题是,我应该使用非常快的轻量级(不需要SAX等)XML解析器(任何想法?)或写我自己的,基本上转换成一个StringBuffer并旋转数组?基本上,在封面下,我假定所有的HTML解析器都将通过字符串(或内存缓冲区)和解析来转换,从而产生输出。



谢谢



编辑



xml将在3/4行到最大50个之间(在极限)..

解决方案

不,您不应该尝试为此编写自己的XML解析器。



SAX本身非常轻便和快速,所以我不知道为什么认为这太多了。实际上,使用字符串缓冲区实际上可以使用SAX更少的可扩展性,因为SAX不需要将整个XML文件加载到内存中以使用它。我使用SAX来解析多GB的XML文件,您将无法在32位机器上使用字符串缓冲区。



如果您有小文件并且您不需要担心性能,请查看使用DOM。 Java的实现可能会令人厌烦(您使用DocumentBuilderFactory中的DocumentBuilder创建文档)



从文件创建文档的代码看起来像这样:

 文档d = DocumentBuilderFactory.newInstance()。newDocumentBuilder()。parse(new FileInputStream(file.xml )); 

(请注意,如果需要解析多个文件,请保持对文档构建器的引用将加快速度)



然后使用 org.w3c.dom.Document 读取或操纵内容。例如 getElementsByTagName()返回所有具有特定标签名称的元素。


I have a specific format XML document that I will get pushed. This document will always be the same type so it's very strict.

I need to parse this so that I can convert it into JSON (well, a slightly bastardized version so someone else can use it with DOJO).

My question is, shall I use a very fast lightweight (no need for SAX, etc.) XML parser (any ideas?) or write my own, basically converting into a StringBuffer and spinning through the array? Basically, under the covers I assume all HTML parsers will spin thru the string (or memory buffer) and parse, producing output on the way through.

Thanks

edit

The xml will be between 3/4 lines to about 50 max (at the extreme)..

解决方案

No, you should not try to write your own XML parser for this.

SAX itself is very lightweight and fast, so I'm not sure why think it's too much. Also using a string buffer would actually be much less scalable then using SAX because SAX doesn't require you to load the whole XML file into memory to use it. I've used SAX to parse through multigigabyte XML files, which you wouldn't be able to do using string buffers on a 32 bit machine.

If you have small files and you don't need to worry about performance, look into using the DOM. Java's implementation can be kind of annoying to use (You create a document by using a DocumentBuilder, which comes from a DocumentBuilderFactory)

The code to create a document from a file looks like this:

Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new FileInputStream("file.xml"));

(note that keeping a reference to your document builder will speed things up if you need to parse multiple files)

Then you use the function in org.w3c.dom.Document to read or manipulate the contents. For example getElementsByTagName() returns all the Elements with a certain tag name.

这篇关于快速,轻量级的XML解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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