在 Java 中读取 XML 的最佳方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7188101/
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-10-30 19:00:09  来源:igfitidea点击:

Best way to read XML in Java

javaxml

提问by Romani

From some of our other application i am getting XML file.

从我们的其他一些应用程序中,我得到了 XML 文件。

I want to read that XML file node by node and store node values in database for further use.

我想逐个节点读取该 XML 文件并将节点值存储在数据库中以供进一步使用。

So, what is the best way/API to read XML file and retrieve node values using Java?

那么,使用 Java 读取 XML 文件和检索节点值的最佳方式/API 是什么?

采纳答案by Andreas Dolk

dom4jand jdomare pretty easy to use (ignoring the requirement "best" for a moment ;) )

dom4jjdom非常易于使用(暂时忽略“最佳”要求;))

回答by Aaron Digulla

There are various tools for that. Today, I prefer two:

有各种工具可以做到这一点。今天,我更喜欢两个:

Here is a good comparison between the Simple and JAXB: http://blog.bdoughan.com/2010/10/how-does-jaxb-compare-to-simple.html

这是 Simple 和 JAXB 之间的一个很好的比较:http: //blog.bdoughan.com/2010/10/how-does-jaxb-compare-to-simple.html

Personally, I like Simple a bit better because support by Niall is excellent but JAXB (as explained in the blog post above) can produce better output with less code.

就我个人而言,我更喜欢 Simple 一点,因为 Niall 的支持非常好,但 JAXB(如上面的博客文章中所述)可以用更少的代码产生更好的输出。

StAX is a more basic API which allows you to read XML documents that simply don't fit into RAM (neither Simple nor JAXB allow you to read an XML document "object by object" - they will always try to load everything into RAM at once).

StAX 是一个更基本的 API,它允许您读取根本不适合 RAM 的 XML 文档(Simple 和 JAXB 都不允许您“逐个对象”地读取 XML 文档——它们总是会尝试一次将所有内容加载到 RAM 中) )。

回答by Farmor

I would advice for a simple XML tool if you can manage by that.

如果您可以通过它进行管理,我会建议使用一个简单的 XML 工具。

For example I and my colleges have introduces complex XML frameworks that worked like a charm at first. Then you forget about the framework, you have special build files just for mapping XML to beans, you have annotated beans, you provide a new barrier for new developers to your project. You loose much of your freedom to refactor.

例如,我和我的大学引入了复杂的 XML 框架,这些框架起初非常有用。然后你忘记了框架,你有专门用于将 XML 映射到 bean 的构建文件,你有注释的 bean,你为项目的新开发人员提供了一个新的障碍。你失去了重构的大部分自由。

At the end you will be sorry that you used the complex framework to save some time in the beginning and I have seen more than one time that the frameworks have been thrown out in refactoring because everybody had a negative feeling about it although they are great at paper.

最后你会后悔你一开始使用复杂的框架来节省一些时间,我已经看到不止一次框架在重构中被抛弃了,因为每个人都对它有负面的感觉,尽管他们很擅长纸。

So think twice about introducing complex XML frameworks if you seldom use them. If you and your team use them rather frequently then they are the way to go.

因此,如果您很少使用复杂的 XML 框架,请三思而后行。如果您和您的团队经常使用它们,那么它们就是您要走的路。

回答by forty-two

I suggest using XPath. Xalan is already included in the JDK (no external jars needed) and it fits your requirement, i.e. iterating through element nodes (i presume) and storing their text values. For example:

我建议使用 XPath。Xalan 已经包含在 JDK 中(不需要外部 jars)并且它符合您的要求,即遍历元素节点(我假设)并存储它们的文本值。例如:

    String xml = "<root> <item>One</item> <item>Two</item> <item>Three</item> </root>";

    XPathFactory xpf = XPathFactory.newInstance();
    InputSource is = new InputSource(new StringReader(xml));
    NodeList nodes = (NodeList) xpf.newXPath().evaluate("/*/*", is,
            XPathConstants.NODESET);
    for (int i = 0; i < nodes.getLength(); ++i) {
        Element e = (Element) nodes.item(i);
        System.out.println(e.getNodeName() + " -> " + e.getTextContent());
    }
}

This example returns a list of all non-root elements and print out the corresponding element name and text content. Adapt the xpath expression to fit your needs.

此示例返回所有非根元素的列表并打印出相应的元素名称和文本内容。调整 xpath 表达式以满足您的需要。

回答by Wivani

Bypassing alltogether the question of parsing the xml and storing the values in a database, I'd like to question the need to do the above. Most databases can handle xml nowadays, so it can be stored in some way into a table without the need of parsing the content; and the content of such an xml within a column in a table can typically be queried by use of 'xmlselect()' and similar functions.

完全绕过解析 xml 并将值存储在数据库中的问题,我想质疑是否需要执行上述操作。现在大多数数据库都可以处理xml,因此可以以某种方式将其存储到表中,而无需解析内容;并且通常可以使用“xmlselect()”和类似函数来查询表中列中此类 xml 的内容。

Think about this for a second; if in the near or distant future the content of the xml that you get from the other application changes, you'll have plenty of changes to do. If it changes often, it'll become a nightmare.

想一想;如果在不久或遥远的将来,您从其他应用程序获得的 xml 的内容发生了变化,您将有大量的更改要做。如果它经常变化,它将成为一场噩梦。

Cheers, Wim

干杯,维姆

回答by Oh Chin Boon

Try Apache Xerces. It is mature and robust. Any such available alternatives will do also, just be sure not to roll out your own implementation.

试试Apache Xerces。它成熟而健壮。任何此类可用的替代方案也可以,只是确保不要推出您自己的实现。

回答by Infeligo

Try XStream, this one's really simple.

试试XStream,这个真的很简单。

回答by David

well,i used stax to parse quite a huge of XML nodes, which consumes less memory than Dom and sax, cauz it is of style of pulling XML data. Stax might be a good choice for large XML data nodes.

嗯,我用 stax 解析了相当多的 XML 节点,比 Dom 和 sax 消耗更少的内存,因为它是拉取 XML 数据的风格。对于大型 XML 数据节点,Stax 可能是一个不错的选择。