Depends on what you want.  You just want a list of sorted nodes, or do 
you want them to be in sorted order in the document?

Here's some psudocode that uses insertion sort to build a list of sorted 
elements:
Element main = (Element)document.selectSingleNode("//main");
List sorted = new ArrayList(main.elements().size());
for(Iterator it = main.elements().iterator(); it.hasNext(); ){
    Element current = it.next();
    int id = Integer.parseInt(current.attributeValue("id"));
    for(int i = 0; i<sorted.size(); i++){
        int sortedId = ( (Element)sorted.get(i) ).attributeValue("id");
        if( id > sortedId ){
          sorted.add(current, i+1);
       }else if(i == 0){
          sorted.add(current, 0);
       }
    }
}

If you want to sort them in place in the document, you could use bubble 
sort or something.
--Evan


Krishnam Raju wrote:
>  Hi All,
>
>  Below is the sample XML and i want to sort it based on "id" attribute that
> is available in the "key" element.
>
>  <?xml version="1.0" encoding="ISO-8859-1"?>
> <main>
>       <key id="24">
>               <insert>
>                       <triggername>ACTIVITY_TRIG</triggername>
>                       <schema>ACTIVITY</schema>
>               </insert>
>       </key>
>       <key id="47">
>               <update>
>                       <triggername>POLICY.RISK_TRIG</triggername>
>                       <schema>POLICY.RISK</schema>
>               </update>
>       </key>
>       <key id="70">
>               <insert>
>                       <triggername>ACTIVITY_TRIG</triggername>
>                       <schema>ACTIVITY</schema>
>                       <cascade/>
>               </insert>
>       </key>
>       <key id="192">
>               <update>
>                       <triggername>POLICY.GENERAL_TRIG</triggername>
>                       <schema>POLICY.GENERAL</schema>
>               </update>
>       </key>
>       <key id="93">
>               <update>
>                       <triggername>POLICY.GENERAL_TRIG</triggername>
>                       <schema>POLICY.GENERAL</schema>
>                       <cascade/>
>               </update>
>       </key>
> </main>
>
>  I have written the below code to do sorting.
>
>
>       SAXReader reader = new SAXReader();
>       Document document = reader.read( new File( "d:/test1.xml" ) );
>       List speakers = document.selectNodes( "//main/key", "@id");
>
>       for(int i=0;i<speakers.size();i++)
>       {
>          Element el = (Element)speakers.get(i);
>          System.out.println(el.attributeValue("id"));
>       }
>
>  and the output i got was
>
>  192
>  24
>  47
>  70
>  93
>
>  I tried different alternatives but i was not able to sort this in ascending
> order.
>
>  Please advice.
>
>  Thanks,
>  Raju
>
>
> SPAN Systems Corporation, Bangalore. 
> "Steering Progress, Together" 
> ===================================================
> This email message and any attachments is confidential and 
> intended only for the use of an individual or entity named above 
> and may contain information that is privileged, confidential or 
> exempt from disclosure under applicable law. If you are not the 
> intended recipient, you are notified that any dissemination, 
> distribution or copying of this email is strictly prohibited.If you 
> have received this email in error, please notify us immediately 
> by return email or [EMAIL PROTECTED] and destroy the 
> original message. Opinions, conclusions, and other information 
> in this message that do not relate to the official business of SPAN, 
> shall be understood to be neither given nor endorsed by SPAN.
>
>
>
>
> -------------------------------------------------------------------------
> Take Surveys. Earn Cash. Influence the Future of IT
> Join SourceForge.net's Techsay panel and you'll get the chance to share your
> opinions on IT & business topics through brief surveys - and earn cash
> http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
> _______________________________________________
> dom4j-user mailing list
> dom4j-user@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/dom4j-user
>   


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
dom4j-user mailing list
dom4j-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to