tags:

views:

49

answers:

2

I have a xml file like,

<step_list Number="5">
    <step Program="P1" Step="STEP01" Seq="1">
    </step>
    <step Program="P2" Step="STEP02" Seq="3">
    </step>
    <step Program="P3" Step="STEP03" Seq="2">
    </step>
    <step Program="P4" Step="STEP04" Seq="5">
    </step>
    <step Program="P5" Step="STEP05" Seq="4">
    </step>
</step_list>

I want a way to read this file with ascending order of seq number. Can you give a clue about how is that possible in jQuery ?

A: 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>test xml</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

(function($)
{
    $.string2xml = function(text)
    {
     var xmlDoc = "";

     if (window.DOMParser)
     {
      parser = new DOMParser();
      xmlDoc = parser.parseFromString(text,"text/xml");
     }
     else // Internet Explorer
     {
      xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async="false";
      xmlDoc.loadXML(text); 
     }

     return xmlDoc;
    };

})(jQuery);

$(function()
{
    var sxml = "";
    sxml += '<?xml version="1.0" encoding="iso-8859-1"?>'+
    '<step_list Number="5">' +
    '    <step Program="P1" Step="STEP01" Seq="1">1</step>' +
    '    <step Program="P2" Step="STEP02" Seq="3">2</step>' +
    '    <step Program="P3" Step="STEP03" Seq="2">3</step>' +
    '    <step Program="P4" Step="STEP04" Seq="5">5</step>' +
    '    <step Program="P5" Step="STEP05" Seq="4">4</step>' +
    '    <step Program="P4" Step="STEP04" Seq="6">6</step>' +
    '    <step Program="P4" Step="STEP04" Seq="9">9</step>' +
    '    <step Program="P4" Step="STEP04" Seq="10">10</step>' +
    '    <step Program="P4" Step="STEP04" Seq="7">7</step>' +
    '    <step Program="P4" Step="STEP04" Seq="8">8</step>' +
    '</step_list>';

    var oxml = $.string2xml(sxml);

    for(var ind = 1; ind < 11; ind++)
    {
     var step = $(oxml).find("step[Seq='"+ind+"']");
     if (step.length > 0)
      $("#resultado").append("<div>"+$(step).attr("Seq")+"</div>");
    }

});

</script>
</head>
<body>
<div id="resultado"></div>
</body>
</html>
andres descalzo
Will it be equally performing as the each function does ? I can not afford to loose any performance !
Indraneel
I guess so, anyway you should test with plenty of data to see if it affects the performance
andres descalzo
A: 
var seq = new Array();

$(xml).find('step_list').each( function() {
  seq[ $(this).attr('Seq') ] = $(this).attr('Step');
} );

So you have an array :)

silent