0

The order of the attributes changes of using XmlSlurper.

Why it changes ?

We know that it doesn't affect the xml grammar. Even Is there any methods to keep the order as it is (or) Is there any methods to control the order ?

Input:

<root>
<Row Title="n1" Author="a" Genre="s"/>
<Row Title="n2" Author="b" Genre="d"/>
<Row Title="n3" Author="c" Genre="s"/>
<Row Title="n4" Author="d" Genre="f"/>
<Row Title="n5" Author="e" Genre="s"/>
<Row Title="n6" Author="f" Genre="a"/>
<Row Title="n7" Author="g" Genre="d"/>
<Row Title="n8" Author="h" Genre="d"/>
<Row Title="n9" Author="I" Genre="d"/>
</root>

Observed: After Parsed using the groovy code given below,

def root = new XmlSlurper().parse(inputfile)
root.children().each { child ->
        println child.attributes()
}

[Author:a, Title:n1, Genre:s]
[Author:b, Title:n2, Genre:d]
[Author:c, Title:n3, Genre:s]
[Author:d, Title:n4, Genre:f]
[Author:e, Title:n5, Genre:s]
[Author:f, Title:n6, Genre:a]
[Author:g, Title:n7, Genre:d]
[Author:h, Title:n8, Genre:d]
[Author:I, Title:n9, Genre:d]

Why it comes like this ?

Bhanuchander Udhayakumar
  • 1,581
  • 1
  • 12
  • 30
  • 1
    In XML the order of the attributes is not relevant, therefore it isn't guaranteed that any parser maintains the order of the input. – Stephan Jul 31 '17 at 08:53
  • Thanks for your response @Stephan. Then how can i get in exact format like my input xml file ? – Bhanuchander Udhayakumar Jul 31 '17 at 08:59
  • Or is it not a problem even the order changes with in that same row? – Bhanuchander Udhayakumar Jul 31 '17 at 09:05
  • (Why) do you have any code which depends on the attribute's order? – Stephan Jul 31 '17 at 09:20
  • @Bhanuchander_U, what are you trying to do actually? – Rao Jul 31 '17 at 09:28
  • @Rao, My Expected output should be like this [Title:n1, Author:a, Genre:s] [Title:n2, Author:b, Genre:d] [Title:n3, Author:c, Genre:s] [Title:n4, Author:d, Genre:f] [Title:n5, Author:e, Genre:s] [Title:n6, Author:f, Genre:a] [Title:n7, Author:g, Genre:d] [Title:n8, Author:h, Genre:d] [Title:n9, Author:i, Genre:d] – Bhanuchander Udhayakumar Jul 31 '17 at 09:47
  • @Stephan - Actually i want to update the xml file with a csv file which is having some attributes including the same attributes shown in xml file like Title : 'n1'. I m using maps to do this. After parsing the xml file the actual format of xml file is changed so the updated xml file format seems like changed. Sorry I dnt know much about this, Is there any way to keep that in same format after parsing? Thanks for ur responses :) – Bhanuchander Udhayakumar Jul 31 '17 at 10:03
  • 1
    But in that case the order of the attributes is not relevant, the updated XML file doesn't have to have the same order as the original one. – Stephan Jul 31 '17 at 10:07
  • @Bhanuchander_U, thank you for the reply. But you did not reply to the question asked. – Rao Jul 31 '17 at 10:13
  • @Stephen, Its okay dude :) I got it. It will be in any order. But I just want to know, is there any way to keep the same order ? Or Making that all attributes in our required order? Sry for asking again and again about keeping the same orders :) coz its my requirement. – Bhanuchander Udhayakumar Jul 31 '17 at 10:16
  • @Rao Sorry I m not getting to u r point. I already told the input which was shown above that xml file. While i am parsing with that XmlSlurper(), the order of attributes are changes. I want to keep the attributes in the same order while i am parsing with XmlSlurper(). Thats what i m trying to do. Hope u got it :) – Bhanuchander Udhayakumar Jul 31 '17 at 10:30
  • 1
    @Bhanuchander_U, that was already available in the question and understood. what is the actual issue because of different order? or your use case? – Rao Jul 31 '17 at 10:33
  • @Rao : Thanks for ur response. There is no issue occurs regarding that. I can access the node and attributes without any problem. Its okay. But simply i want to know that, Can i make it exactly as same as the input xml format. Coz i am going to generate another one xml with Markupbuilder. If i read in same order, I can generate another one xml file which will be as same as the input xml file. Thats why i am asking for that. :) – Bhanuchander Udhayakumar Jul 31 '17 at 10:51
  • @Bhanuchander_U, It would appear different for us. But it is no different for the parsers which was already reiterated by other community members. – Rao Jul 31 '17 at 10:53
  • @Rao K Thanks for ur responses. Since now I am not going to worry about the order of attributes. Thank u :) – Bhanuchander Udhayakumar Jul 31 '17 at 11:04
  • Also note, that `attributes()` returns a subclass of `HashMap` - so you allready lost the order there. One of the reasons to keep the order in XML i can imagine is to do text manipulation or diffs. So if you give up the properties of XML at that end and fall back to simple text manipulation you might as well give it up in your "enhancing" step here and go with text manipulation. Not a great idea imho – cfrick Jul 31 '17 at 15:16
  • @cfrick Thanks for ur suggestions. :) – Bhanuchander Udhayakumar Jul 31 '17 at 15:37

0 Answers0