编写XML元素时设置属性的顺序 [英] Setting the order of attribute when writing XML Element

查看:904
本文介绍了编写XML元素时设置属性的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java中编写以下代码时:

When writing in java the following:

Element fieldEl = targetDocument.createElement("field");
fieldEl.setAttribute("Wine","Marlo");
fieldEl.setAttribute("Beer","Corona");

添加属性的顺序不保存在结果XML文件中。

如何控制XML元素中属性的顺序(因此人类很容易阅读...)??

The order of adding the attributes are not kept in the result XML file.
How can I control the order of the attribute inside XML Element (so it will be easy for human being to read...) ??

推荐答案

根据 DOM标准


对象实现NamedNodeMap接口用于表示可以按名称访问的节点集合。注意,NamedNodeMap不从NodeList继承; NamedNodeMaps不以任何特定顺序维护。 实现NamedNodeMap的对象中包含的对象也可以通过序数索引访问,但这只是为了方便枚举NamedNodeMap的内容,并不意味着DOM指定了对这些节点的顺序。

(强调添加),而且 XML标准


注意start-tag或empty-element标签中属性规范的顺序并不重要。

Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.

我不认为,那个许多DOM实现都支持属性排序。你必须编写自己的序列化机制才能实现排序(没有双关语)。

I don't think, that many DOM implementations support ordering of attributes at all. You'd have to write your own serialization mechanism in order to achieve ordering (no pun intended).

这篇关于编写XML元素时设置属性的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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