有没有更简单的方法来解析 Java 中的 XML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1719261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:51:55  来源:igfitidea点击:

Is there an easier way to parse XML in Java?

javaxmlandroid

提问by Kyle Slattery

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(对于 Android 应用程序),而在 Java 中这样做的难度似乎非常荒谬。似乎它需要创建一个具有各种回调(startElement、endElement 等)的 XML 处理程序,然后您必须负责将所有这些数据更改为对象。类似这个教程的东西。

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

我真正需要的是将 XML 文档更改为多维数组,甚至更好的是拥有某种Hpricot处理器。有没有办法做到这一点,或者我真的必须在上面的例子中编写所有额外的代码?

采纳答案by stimms

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.

Java 中有两种不同类型的 XML 处理器(实际上有 3 种,但一种很奇怪)。您拥有的是 SAX 解析器,而您想要的是 DOM 解析器。查看http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/ 了解如何使用 DOM 解析器。DOM 将创建一棵树,您可以很容易地浏览它。SAX 最适合大型文档,但如果速度较慢且内存密集程度更高,则 DOM 会容易得多。

回答by Hank Gay

Starting w/ Java 5, there is an XPath library in the SDK. See this tutorialfor an introduction to it.

从 Java 5 开始,SDK 中有一个 XPath 库。有关的介绍,请参阅本教程

回答by James Anderson

Well parsing XML is not an easy task.

解析 XML 并不是一件容易的事。

Its basic structure is a tree with any node in tree capable of holding a container which consists of an array of more trees.

它的基本结构是一棵树,树中的任何节点都可以容纳一个由更多树组成的数组的容器。

Each node in a tree contains a tag and a value but in addtion can contain an arbitary number of named attributes, and, an arbitary number of children or containers.

树中的每个节点都包含一个标签和一个值,但此外还可以包含任意数量的命名属性,以及任意数量的子节点或容器。

XML parsing tasks tend to fall in to three catagories.

XML 解析任务往往分为三类。

Things that can be done with "regex". E.g. you want to find the value of the first "MailTo" tag and are not interested in the contents of any other tags.

可以用“正则表达式”完成的事情。例如,您想找到第一个“MailTo”标签的值,而对任何其他标签的内容不感兴趣。

Things you can parse yourself. The xml structure is always very simple e.g a root node and ten well known tags with simple values.

你可以自己解析的东西。xml 结构总是非常简单,例如一个根节点和十个具有简单值的众所周知的标签。

All the rest! Even though an xml message format can look deceptively simple home made parsers are easily confused by extra attributes, CDATA and unexpected children. Full blown XML parsers can handle all of these situations. Here the basic choice is between a stream or a DOM parser. If you intend to use most of the entities/attributes given in the order you want to use them then a DOM parser is ideal. If you are only interested in a few attributes and intend to use them in the order they are presented, if you have performance constraints, or, if the xml files are large ( > 500MB ) than a stream parser is the way to go; the callback mechanism takes a bit of "groking" but its actually quite simple to program once you get the hang of it.

其他的!尽管 xml 消息格式看起来很简单,但自制的解析器很容易被额外的属性、CDATA 和意外的子项混淆。成熟的 XML 解析器可以处理所有这些情况。这里的基本选择是在流或 DOM 解析器之间。如果您打算按照您想要使用的顺序使用大多数实体/属性,那么 DOM 解析器是理想的选择。如果您只对几个属性感兴趣并打算按照它们的显示顺序使用它们,如果您有性能限制,或者如果 xml 文件很大(> 500MB),那么流解析器是一种可行的方法;回调机制需要一些“摸索”,但一旦掌握了它,它实际上很容易编程。

回答by misamap

Acording to me, you should use SAX parser because: - Fast - you can control everything in XML document

根据我的说法,您应该使用 SAX 解析器,因为: - 快速 - 您可以控制 XML 文档中的所有内容

You will pay more time to coding, but it's once because you will create code template to parse XML

你会花更多的时间来编码,但这是一次,因为你将创建代码模板来解析 XML

From second case, you only edit content of changes.

在第二种情况下,您只编辑更改的内容。

Good luck!

祝你好运!

回答by Rahul

You could also use Castorto map the XML to Java beans. I have used it before and it works like a charm.

您还可以使用Castor将 XML 映射到 Java bean。我以前用过它,它的作用就像一个魅力。

回答by Bostone

Writing SAX handleris the best way to go. And once you do that you will never go back to anything else. It's fast, simple and it crunches away as it goes, no sucking large parts or god forbid a whole DOM into memory.

写作SAX handler是最好的方法。一旦你这样做了,你将永远不会回到其他任何事情上。它快速、简单,而且它会随着它的运行而嘎吱作响,不会将大的部分或上帝禁止将整个 DOM 放入内存中。

回答by ng.

Try http://simple.sourceforge.net, its an XML to Java serialization and binding framework, its fully compatible with Android and is very lightweight, 270K and no dependencies.

试试http://simple.sourceforge.net,它是一个 XML 到 Java 的序列化和绑定框架,它与 Android 完全兼容并且非常轻量级,270K 并且没有依赖项。

回答by jaz303

A couple of weeks ago I battered out a small library (a wrapper around javax.xml.stream.XMLEventReader) allowing one to parse XML in a similar fashion to a hand-written recursive descent parser. The source is available on github, and a simple usage example is below. Unfortunately Android doesn't support this API but it is very similar to the XmlPullParserAPI, which is supported, and porting wouldn't be too time-consuming.

几周前,我开发了一个小型库(一个包装器javax.xml.stream.XMLEventReader),它允许以类似于手写递归下降解析器的方式解析 XML。源码可在 github 上找到,下面是一个简单的使用示例。不幸的是,Android 不支持此 API,但它与支持的 API 非常相似XmlPullParser,移植不会太耗时。

accept("tilesets");
    while (atTag("tileset")) {
        String filename = attrib("file");
        File tilesetFile = new File(filename);
        if (!tilesetFile.isAbsolute()) {
            tilesetFile = new File(FilenameUtils.concat(file.getParent(), filename));
        }
        int tilesize = Integer.valueOf(attrib("tilesize"));
        Tileset t = new Tileset(tilesetFile, tilesize);
        t.setID(attrib("id"));
        tilesets.add(t);

        accept();
        close();
    }
close();

expect("map");

int width       = Integer.valueOf(attrib("width"));
int height      = Integer.valueOf(attrib("height"));
int tilesize    = Integer.valueOf(attrib("tilesize"));

回答by M.Bearden

In my opinion, using XPath for parsing XMLmay be your easiest coding approach. You can embody the logic for pulling out nodes from an XML document in a single expression, rather than having to write the code to traverse the document's object graph.

在我看来,使用XPath 解析 XML可能是最简单的编码方法。您可以在单个表达式中体现从 XML 文档中提取节点的逻辑,而不必编写代码来遍历文档的对象图。

I note that another posted answer to this questionalready suggested using XPath. But not yet for your Android project. As of right now, the XPath parsing class is not yet supported in any Android release(even though the javax.xml namespace is defined in the Dalvik JVM, which could fool you, as it did me at first).

我注意到这个问题的另一个发布的答案已经建议使用 XPath。 但尚未用于您的 Android 项目。到目前为止,任何 Android 版本都不支持 XPath 解析类(即使 javax.xml 命名空间是在 Dalvik JVM 中定义的,这可能会欺骗您,就像我最初所做的那样)。

Inclusion of XPath class in Android is a current work item in late phase. (It is being tested and debugged by Google as I write this). You can track the status of adding XPath to Davlik here: http://code.google.com/p/android/issues/detail?id=515

在 Android 中包含 XPath 类是后期阶段的当前工作项。(在我撰写本文时,它正在由 Google 进行测试和调试)。您可以在此处跟踪将 XPath 添加到 Davlik 的状态http: //code.google.com/p/android/issues/detail? id=515

(It's an annoyance that you cannot assume things supported in most Java VMs are included yet in the Android Dalvik VM.)

(令人烦恼的是,您不能假设 Android Dalvik VM 中还包含大多数 Java VM 支持的内容。)

Another option, while waiting for official Google support, is JDOM, which presently claims Dalvik VM compatibility and also XPath support (in beta). (I have not checked this out; I'm just repeating current claims from their web site.)

在等待 Google 官方支持时,另一个选择是JDOM,它目前声称与 Dalvik VM 兼容并支持 XPath(测试版)。(我没有检查过这个;我只是在重复他们网站上的当前声明。)

回答by Chris

I've created a really simple API to solve precisely this problem. It's just a single class that you can include in your code base and it's really clean and easy to parse any XML. You can find it here:

我创建了一个非常简单的 API 来精确解决这个问题。它只是一个可以包含在代码库中的类,而且它非常干净且易于解析任何 XML。你可以在这里找到它:

http://argonrain.wordpress.com/2009/10/27/000/

http://argonrain.wordpress.com/2009/10/27/000/