Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read XML Node with two attribures without changing their sequence

 
Java Pradeep Kumar Kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a requirement to read XML file in which Node is having two attributes with values. I have to read this Node and its two attribute as it appears in XML file(eg:confirm="Always" acknowledge="Always) and print in Excel file.

The code for this requirement I have written is as follows

if(xmlfile.contains("Process "))
{
splitValue=xmlfile.split("Process ");
System.out.println(splitValue[1]);
}

Output I am getting is : acknowledge="Always" confirm="Always". it is showing attributes in alphabetical order. But in xmlfile it is :confirm="Always" acknowledge="Always".

I want to print the attributes as they appear in xmlfile(confirm="Always" acknowledge="Always").

Please help on this issue. Help will be appreciated.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the XML specification, you have no control over the order of attributes inside a Node or inside the collection that getAttributes() gives you.

Therefore: if you want them in a particular order you will have to program extracting them from the collection by name.

Bill


 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and what that means is that the two forms are semantically equivalent, and should not be treated as if they were different in any way - that would violate the XML specification (and also the way all XML tools handle XML).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic