Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

XML sorting

Hi. I have an XML file. I have loaded it as a document and am trying to sort the XML. Can anyone let me know which is the best way to sort an XML file?
0
PraKash
Asked:
PraKash
  • 10
  • 8
1 Solution
 
CEHJCommented:
For what purpose do you wish to sort it?
0
 
PraKashAuthor Commented:
I wish to sort it by an attribute value in assescending order.
0
 
PraKashAuthor Commented:
Actually I need to work on that XML file as an input for manipulating values in another database. So this does not involve any web based application. Just core java.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
CEHJCommented:
Did this before you said about attributes, but you can use excatly the same principle:

<nodes>
      <a>b</a>
      <a>z</a>
      <a>a</a>
</nodes>      

....

NodeList list = documentNode.getElementsByTagName("a");
Collections.sort(list, new XmlSorter());


public XmlSorter implements Comparator {
      public int compare(Object o1, Object o2) {
            Node n1 = (Node)o1;
            Node n2 = (Node)o2;
            return n1.getFirstChild().getNodeValue().compareTo(return n1.getFirstChild().getNodeValue());
      }
}      
0
 
CEHJCommented:
>>return n1.getFirstChild().getNodeValue().compareTo(return n1.getFirstChild().getNodeValue());

should be


return n1.getFirstChild().getNodeValue().compareTo(n2.getFirstChild().getNodeValue());
0
 
PraKashAuthor Commented:
says documentNode is not a valid document. Well this is my last code
Document taskmod = XMLUtil.loadXML("asd.xml");
After this what should be done? Can u help?
0
 
PraKashAuthor Commented:
Can XSL be used to sort the xml file?
I tried using XSL over the XML and tried transforming but got an empty document
sample xml file
<aaa>
<aa b="z">
   <a bb=1>
</aa>
<aa b="y">
   <a bb=2>
</aa>
</aaa>
I need to sort the aa columns in assending order.
0
 
CEHJCommented:
>>says documentNode is not a valid document

That's just an example. In your case, it would be 'taskMod'

>>Can XSL be used to sort the xml file?

Only at a 'presentation' level. Is that what you want? (This is why i was asking you what you wanted to do this *for*)

0
 
PraKashAuthor Commented:
First to refer to that documentNode, it says method undefined for type Document. Thats why I asked if this was another class as am currently refering to the dom4j document.

I am not using this for the presentation level. I have used XSL to simply have a markup of how the XML documents should look like in my previous occations. I am using this to format the XML so that I can use it elsewhere in the program to group certain files and write it as a different XML file itself.
0
 
CEHJCommented:
>>I am not using this for the presentation level.

Then you should probably forget the xsl stuff
0
 
PraKashAuthor Commented:
OK. Then tell me how to do it Java. I tried what u had asked me to. Like I said it is giving the above error.
Can you please help me out in this.
0
 
CEHJCommented:
You need to call getElementsByTagName on a valid Document reference
0
 
PraKashAuthor Commented:
Well it is refering to a dom4j Document reference. If it is the same kind of reference u r talkin abt. I am able to print it asXML and everything.
Can u please tell me which class's document will contain the method getElementsByTagName?
0
 
PraKashAuthor Commented:
I think this method is available only for w3c.dom document and not dom4j doument.
0
 
CEHJCommented:
>>Well it is refering to a dom4j Document reference.

In that case try

List list = taskMod.selectNodes("a");
Collections.sort(list, new XmlSorter());
0
 
PraKashAuthor Commented:
public XmlSorter implements Comparator {

Syntax errors on tokens. What does that mean?
0
 
PraKashAuthor Commented:
and how to convert nodes back to a XML document
0
 
CEHJCommented:
>>Syntax errors on tokens. What does that mean?

Maybe you're calling the code i posted wrongly try:

NodeList list = documentNode.getElementsByTagName("a");
Collections.sort(list, new Comparator() {
      public int compare(Object o1, Object o2) {
            Node n1 = (Node)o1;
            Node n2 = (Node)o2;
            return n1.getFirstChild().getNodeValue().compareTo(n2.getFirstChild().getNodeValue());
      }
});

>>and how to convert nodes back to a XML document

This really depends on the structure of your original and what you want the resulting structure to be
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now