使用 javax.xml.transform.Transformer 对 xml 属性进行排序以实现漂亮的打印

发布于 2025-01-03 22:35:36 字数 928 浏览 0 评论 0原文

有没有办法告诉 xml 转换器按字母顺序对给定 XML 标签的所有属性进行排序?所以可以说...

<MyTag paramter1="lol" andTheOtherThing="potato"/>

会变成

<MyTag andTheOtherThing="potato" paramter1="lol"/>

我看到如何从我找到的示例中格式化它 此处此处,但对标签属性进行排序将是我遇到的最后一个问题。

我希望有类似的东西:

transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.SORTATT, "yes"); // <-- no such thing

他们似乎是这么说的: http://docs.oracle。 com/javase/1.4.2/docs/api/javax/xml/transform/OutputKeys.html

Is there a way I could tell the xml transformer to sort alphabetically all the attributes for the tags of a given XML? So lets say...

<MyTag paramter1="lol" andTheOtherThing="potato"/>

Would turn into

<MyTag andTheOtherThing="potato" paramter1="lol"/>

I saw how to format it from the examples I found here and here, but sorting the tag attributes would be the last issue I have.

I was hoping there was something like:

transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.SORTATT, "yes"); // <-- no such thing

Which seems to be what they say:
http://docs.oracle.com/javase/1.4.2/docs/api/javax/xml/transform/OutputKeys.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肤浅与狂妄 2025-01-10 22:35:36

如前所述,到了 42 岁,您可以从 XML 生成规范 XML,这将按字母顺序对属性进行排序。

在 Java 中,我们可以使用 Apache 的 Canonicalizer

org.apache.xml.security.c14n.Canonicalizer

之类的东西(假设 XMLDoc 中的文档已经是 DOM):

Document retDoc;
byte[] c14nOutputbytes;
DocumentBuilderFactory factory;
DocumentBuilder parser;

// CANONICALIZE THE ORIGINAL DOM
c14nOutputbytes = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS).canonicalizeSubtree(inXMLDoc.getDocumentElement());

// PARSE THE CANONICALIZED BYTES (IF YOU WANT ANOTHER DOM) OR JUST USE THE BYTES
factory = DocumentBuilderFactory.newInstance();
factory.set ... // SETUP THE FACTORY
parser = factory.newDocumentBuilder();
// REPARSE TO GET ANOTHER DOM WITH THE ATTRIBUTES IN ALPHA ORDER
ByteArrayInputStream bais = new ByteArrayInputStream(c14nOutputbytes);
retDoc = parser.parse(bais);

当然,在规范化时,其他内容也会发生变化(它将成为 Canonical XML) http://en.wikipedia.org/wiki/Canonical_XML),因此除了属性顺序之外,还需要进行一些更改。

As mentioned, by forty-two, you can make canonical XML from the XML and that will order the attributes alphabetically for you.

In Java we can use something like Apache's Canonicalizer

org.apache.xml.security.c14n.Canonicalizer

Something like this (assuming that the Document inXMLDoc is already a DOM):

Document retDoc;
byte[] c14nOutputbytes;
DocumentBuilderFactory factory;
DocumentBuilder parser;

// CANONICALIZE THE ORIGINAL DOM
c14nOutputbytes = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS).canonicalizeSubtree(inXMLDoc.getDocumentElement());

// PARSE THE CANONICALIZED BYTES (IF YOU WANT ANOTHER DOM) OR JUST USE THE BYTES
factory = DocumentBuilderFactory.newInstance();
factory.set ... // SETUP THE FACTORY
parser = factory.newDocumentBuilder();
// REPARSE TO GET ANOTHER DOM WITH THE ATTRIBUTES IN ALPHA ORDER
ByteArrayInputStream bais = new ByteArrayInputStream(c14nOutputbytes);
retDoc = parser.parse(bais);

Other things will get changed when Canonicalizing of course (it will become Canonical XML http://en.wikipedia.org/wiki/Canonical_XML) so just expect some changes other than the attribute order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文