开发者

Ordered parsing of XML by jquery

开发者 https://www.devze.com 2022-12-10 09:30 出处:网络
I have a xml file like, <step_list Number=\"5\"> <step Program=\"P1\" Step=\"STEP01\" Seq=\"1\">

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"开发者_JAVA百科>
    </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 ?


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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>


var seq = new Array();

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

So you have an array :)

0

精彩评论

暂无评论...
验证码 换一张
取 消