Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1075963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:20:46+00:00 2026-05-16T21:20:46+00:00

When processing XML by means of standard DOM, attribute order is not guaranteed after

  • 0

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.

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.

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.

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.

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.

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.

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:20:47+00:00Added an answer on May 16, 2026 at 9:20 pm

    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 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been doing alot of XML processing in C# of late and after coming
After processing form from POST I should redirect, to prevent user from hitting back.
Using Perl's XML::LibXSLT necessitates that I use XSLT 1.0, which means that I am
I'd like to do some DOM-stlye processing on a very big xml to convert
I really like processing XML with e4x, any other method I just get confused
I am processing an XML file (which does not contain any dtd or ent
I'm processing a XML with minOccurs and maxOccurs set frequently to either 0, 1,
I am processing an xml file using apache.commons.digester The xml is, for instance, structured
I've been doing quite a bit of simple XML-processing in python and grown to
I'm doing some pre-processing of an XML document in a ASMX Webservice (Legacy .NET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.