如何在 Java 中对 XML 元素进行排序? [英] How to sort XML elements in Java?

查看:39
本文介绍了如何在 Java 中对 XML 元素进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据节点对 XML 文档进行排序.什么是有效的排序方法?

I am trying to sort XML documents according to their nodes. What is an efficient sorting method?

推荐答案

我不知道有任何 XML 解析器提供开箱即用的元素排序,因为 XML 元素没有自然的排序顺序.这是因为在 XML 规范中元素的排序顺序很重要,因此解析任意 XML 块的代码不应该对元素的顺序做出任何假设.

I am not aware of any XML parser that provides sorting of elements out of the box since XML elements have no natural sort order. That is because in the XML specification the sort order of elements does matter, therefore no code parsing an arbitrary chunk of XML should make any assumptions about the order of the elements.

如果您需要对元素进行排序,您将不得不使用您最喜欢的 XML 解析器来解析 XML 文档并自己对它们进行排序.或者,您可以使用 XSLT 对文档进行排序.

If you need the elements sorted you are going to have to parse the XML document using your favorite XML parser and sort them yourself. Alternatively, you could sort the document using XSLT.

这篇关于如何在 Java 中对 XML 元素进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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