Order of XML attributes after DOM processing
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 ?".
The short answer is "DOM will not allow you to do that, but SAX will".
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*>
.
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} .../>
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 );
I'd also like to point out, at the intention of many naysayers that there are cases where attribute order does matter.
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 ;-)
Order of XML attributes after DOM processing using c++ with qt
If you are using the DOM-classes of Qt (QDomDocument, ...), then you can't do anything about it, because this happends internally (The attributes are stored in a QHash). The reason: the order of the attributes does not change the meaning of the XML-file, the information contained by the two files is the same.
If you need your attributes to be in the right order, consider using QXmlStreamWriter
- This class will write attributes in the order you pass them - because it is a stream based writer. However, you will have to read and write the data with your own data-structures.
How to order DOM XML elements by number of attributes?
As with most XML transformations, the best tool for the job is XSLT. The following is XSLT 2.0, but it's almost as easy in 1.0
<xsl:template match="/*">
<xsl:perform-sort select="element">
<xsl:sort select="count(@*)" order="descending"/>
</xsl:perform-sort>
</xsl:template>
Java XML library that preserves attribute order
You might also want to try DecentXML, as it can preserve the attribute order, comments and even indentation.
It is very nice if you need to programmatically update an XML file that's also supposed to be human-editable. We use it for one of our configuration tools.
-- edit --
It seems it is no longer available on its original location; try these ones:
- https://github.com/cartermckinnon/decentxml
- https://github.com/haroldo-ok/decentxml (unnoficial and unmaintained fork; kept here just in case the other forks disappear, too)
- https://directory.fsf.org/wiki/DecentXML
Control order of XML attributes in outputed file in Java
I had the same issue when I used XML DOM API for writing file. To resolve the problem I had to use XMLStreamWriter
. Attributes appear in a xml file in the order you write it using XMLStreamWriter
.
Related Topics
Synchronizing on String Objects in Java
Sparse Matrices/Arrays in Java
What Is Lazy Loading in Hibernate
String Concatenation in Java - When to Use +, Stringbuilder and Concat
How to Count Occurrences with Groupby
Parsing Iso-8601 Datetime with Offset with Colon in Java
Determining If an Object Is of Primitive Type
Eclipse: How to Build an Executable Jar with External Jar
Spring Boot CSS Showing Up Blank/Not Loading After Trying Everything
Selenium 2.53 Not Working on Firefox 47
How to Invoke a Linux Shell Command from Java
Using Pairs or 2-Tuples in Java
Getting the Text That Follows After the Regex Match
Build Eclipse Java Project from Command Line
String.Replaceall(Regex) Makes the Same Replacement Twice