有没有更简单的方法来解析XML在Java中? [英] Is there an easier way to parse XML in Java?

查看:143
本文介绍了有没有更简单的方法来解析XML在Java中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何解析一些XML(对于一个Android应用程序),而且看起来pretty的荒谬是多么困难的事在Java中。现在看来似乎需要创建一个XML处理程序有不同的回调(的startElement,endElement和等等),你必须再取更改所有这些数据转化为对象的照顾。像<一个href="http://www.anddev.org/parsing%5Fxml%5Ffrom%5Fthe%5Fnet%5F-%5Fusing%5Fthe%5Fsaxparser-t353.html">this教程。

I'm trying to figure out how to parse some XML (for an Android app), and it seems pretty ridiculous how difficult it is to do in Java. It seems like it requires creating an XML handler which has various callbacks (startElement, endElement, and so on), and you have to then take care of changing all this data into objects. Something like this tutorial.

我真正需要的是一个XML文档转换为一个多维数组,甚至是更好的办法是有某种角度来说,Hpricot 处理器。有没有办法做到这一点,或者说我真的有写所有额外的code在上面的例子?

All I really need is to change an XML document into a multidimensional array, and even better would be to have some sort of Hpricot processor. Is there any way to do this, or do I really have to write all the extra code in the example above?

推荐答案

有两种不同类型的处理器的XML在Java中(3实际上,而是一个很奇怪)。你有什么是SAX解析器和你想要的是一个DOM解析器。看看<一href="http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser">http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/对于如何使用DOM解析器。 DOM将创建您可以轻松地导航pretty的一棵树。 SAX是最适合大文件,但DOM是,如果更慢和更内存密集型容易得多。

There are two different types of processors for XML in Java (3 actually, but one is weird). What you have is a SAX parser and what you want is a DOM parser. Take a look at http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/ for how to use the DOM parser. DOM will create a tree which you can navigate pretty easily. SAX is best for large documents but DOM is much easier if slower and much more memory intensive.

这篇关于有没有更简单的方法来解析XML在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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