DOM处理后的XML属性顺序 [英] Order of XML attributes after DOM processing

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

问题描述

通过标准DOM处理XML时,序列化后不能保证属性顺序。最后,这就是我使用标准的java XML Transform API来序列化输出时所想到的。

When processing XML by means of standard DOM, attribute order is not guaranteed after you serialize back. At last that is what I just realized when using standard java XML Transform API to serialize the output.

然而,我确实需要保留订单。我想知道Java是否有任何可能性,以保持通过DOM API处理的XML文件的属性的原始顺序,或者任何方式强制执行(也许通过使用一个替代的序列化API,让您设置此一种财产)。在我的情况下,处理可以减少使用一系列属性来改变同一个元素序列的一些属性(不是全部)的值,也可能插入一些元素。

However I do need to keep an order. I would like to know if there is any posibility on Java to keep the original order of attributes of an XML file processed by means of DOM API, or any way to force the order (maybe by using an alternative serialization API that lets you set this kind of property). In my case processing reduces to alter the value of some attributes (not all) of a sequence of the same elements with a bunch of attributes, and maybe insert a few more elements.

有没有任何简单的方式,或者我必须定义自己的XSLT转换样式表来指定输出和更改整个输入XML文件?

Is there any "easy" way or do I have to define my own XSLT transformation stylesheet to specify the output and altering the whole input XML file?

更新我必须感谢你的所有答案。答案似乎比我预想的更加明显。我从来没有注意到属性顺序,因为我以前从来没有需要它。

Update I must thank all your answers. The answer seems now more obvious than I expected. I never paid any attention to attribute order, since I had never needed it before.

需要属性顺序的主要原因是生成的XML文件只是看起来不一样。目标是一个配置文件,保存数百个警报(每个警报由一组属性定义)。这个文件通常在一段时间内几乎没有修改,但是很方便地保存它,因为当我们需要修改手工编辑的东西时。现在,然后一些项目需要对该文件进行轻微的修改,例如将其中一个属性设置为客户特定的代码。

The main reason to require an attribute order is that the resulting XML file just looks different. The target is a configuration file that holds hundreds of alarms (every alarm is defined by a set of attributes). This file usually has little modifications over time, but it is convenient to keep it ordered, since when we need to modify something it is edited by hand. Now and then some projects need light modifications of this file, such as setting one of the attributes to a customer specific code.

我刚刚开发了一个小应用程序来将原始文件(所有项目共同)与每个项目的特定部分(修改某些属性的值)进行合并,因此项目特定文件获取基本更新(新的警报定义或某些属性值错误修复)。要求订购属性的主要动机是能够通过文本比较工具(如Winmerge)重新检查原始文件的输出。如果格式(主要是属性顺序)保持不变,则可以很容易地发现差异。

I just developed a little application to merge original file (common to all projects) with specific parts of each project (modify the value of some attributes), so project-specific file gets the updates of the base one (new alarm definitions or some attribute values bugfixes). My main motivation to require ordered attributes is to be able to check the output of the application againts the original file by means of a text comparation tool (such as Winmerge). If the format (mainly attribute order) remains the same, the differences can be easily spotted.

我真的以为这是可能的,因为XML处理程序,如XML Spy ,让您编辑XML文件并应用一些排序(网格模式)。也许我唯一的选择是使用这些程序之一手动修改输出文件。

I really thought this was possible, since XML handling programs, such as XML Spy, lets you edit XML files and apply some ordering (grid mode). Maybe my only choice is to use one of these programs to manually modify the output file.

推荐答案

对不起,但是答案比不,你不能还是为什么你需要首先做这个事情呢?。

Sorry to say, but the answer is more subtle than "No you can't" or "Why do you need to do this in the first place ?".

简单的答案是DOM不会让你这样做,但SAX会。

The short answer is "DOM will not allow you to do that, but SAX will".

这是因为DOM不关心属性顺序,因为它是无意义的作为标准,当XSL保持输入流时,信息已经丢失。
大多数XSL引擎实际上会优雅地保留输入流属性顺序(例如
Xalan-C(除了一种情况)或Xalan-J(总是))。特别是如果您使用< xsl:copy *>

This is because DOM does not care about the attribute order, since it's meaningless as far as the standard is concerned, and by the time the XSL gets hold of the input stream, the info is already lost. Most XSL engine will actually gracefully preserve the input stream attribute order (e.g. Xalan-C (except in one case) or Xalan-J (always)). Especially if you use <xsl:copy*>.

不保存属性顺序的情况,我最了解的是。
- 如果输入流是DOM
- Xalan-C:如果您直接插入结果树标签(例如< elem att1 = {@ att1} ... />

Cases where the attribute order is not kept, best of my knowledge, are. - If the input stream is a DOM - Xalan-C: if you insert your result-tree tags literally (e.g. <elem att1={@att1} .../>

以下是SAX的一个例子,用于记录(抑制DTD叨叨)。

Here is one example with SAX, for the record (inhibiting DTD nagging as well).

SAXParserFactory spf = SAXParserFactoryImpl.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(false);
spf.setFeature("http://xml.org/sax/features/validation", false);
spf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
SAXParser sp = spf.newSAXParser() ;
Source src = new SAXSource ( sp.getXMLReader(), new InputSource( input.getAbsolutePath() ) ) ;
String resultFileName = input.getAbsolutePath().replaceAll(".xml$", ".cooked.xml" ) ;
Result result = new StreamResult( new File (resultFileName) ) ;
TransformerFactory tf = TransformerFactory.newInstance();
Source xsltSource = new StreamSource( new File ( COOKER_XSL ) );
xsl = tf.newTransformer( xsltSource ) ;
xsl.setParameter( "srcDocumentName", input.getName() ) ;
xsl.setParameter( "srcDocumentPath", input.getAbsolutePath() ) ;

xsl.transform(src, result );

我也想指出,根据许多naysayers的意图, 属性顺序 的情况很重要。

I'd also like to point out, at the intention of many naysayers that there are cases where attribute order does matter.

回归测试是一个明显的例子。
谁被调用优化不太好写的XSL知道你通常想要确保新结果树与旧结果类似或相同。当结果树大约在100万行时,XML diff工具证明太笨拙了...
在这些情况下,保留属性顺序是非常有帮助的。

Regression testing is an obvious case. Whoever has been called to optimise not-so-well written XSL knows that you usually want to make sure that "new" result trees are similar or identical to the "old" ones. And when the result tree are around one million lines, XML diff tools prove too unwieldy... In these cases, preserving attribute order is of great help.

希望这有帮助; - )

Hope this helps ;-)

这篇关于DOM处理后的XML属性顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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