Solved

XML sorting

Posted on 2004-10-05
20
328 Views
Last Modified: 2010-03-31
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
Comment
Question by:PraKash
  • 10
  • 8
20 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12231726
For what purpose do you wish to sort it?
0
 

Author Comment

by:PraKash
ID: 12231781
I wish to sort it by an attribute value in assescending order.
0
 

Author Comment

by:PraKash
ID: 12231802
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12231888
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12231895
>>return n1.getFirstChild().getNodeValue().compareTo(return n1.getFirstChild().getNodeValue());

should be


return n1.getFirstChild().getNodeValue().compareTo(n2.getFirstChild().getNodeValue());
0
 

Author Comment

by:PraKash
ID: 12231989
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
 

Author Comment

by:PraKash
ID: 12232093
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12232120
>>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
 

Author Comment

by:PraKash
ID: 12232235
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 86

Expert Comment

by:CEHJ
ID: 12232302
>>I am not using this for the presentation level.

Then you should probably forget the xsl stuff
0
 

Author Comment

by:PraKash
ID: 12232328
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12232365
You need to call getElementsByTagName on a valid Document reference
0
 

Author Comment

by:PraKash
ID: 12232404
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
 

Author Comment

by:PraKash
ID: 12232437
I think this method is available only for w3c.dom document and not dom4j doument.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12232458
>>Well it is refering to a dom4j Document reference.

In that case try

List list = taskMod.selectNodes("a");
Collections.sort(list, new XmlSorter());
0
 

Author Comment

by:PraKash
ID: 12232510
public XmlSorter implements Comparator {

Syntax errors on tokens. What does that mean?
0
 

Author Comment

by:PraKash
ID: 12232517
and how to convert nodes back to a XML document
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 12235775
>>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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now