开发者

Find element in XML using jquery

I'm using jquery to go through a XML file and find some elements and display them on screen

$(xml).find('module').each(function(index){

I would like to select a single element in a certain position. I've tried several options like

$(xml).find('modulo').[1]{

$(xml).find('modulo').(1){

$(xml).find('modulo').each(function(index).get([1]){

$(xml).find('modulo').get([1])

but none as worked so far. Does anyone knows what is the correct sintax?

edit:--------

<script>
    $(document).ready(function(){
        $.ajax({
            type: "GET",
            url: "modulos.xml",
            dataType: "xml",
            success: function(xml) {
                $(xml).find('modulo').each(function(index){
                    var MAC = $(this).find('MAC').text();
                    var Type = $(this).find('Type').text();
                    var Date = $(this).find('Date').text();
                    var Firmware = $(this).find('Firmware').text();
                    var room = $(this).find('room').text();
                    var equipment = $(this).find('equipment').text();
                    var calibration = $(this).find('calibration').text();
                    var next_calibration = $(this).find('next_calibration').text();
                    var rf = $(this).find('rf').text();
                    var battery = $(this).find('battery').text();
                    $('<div class="items" id="link_'+index+'"></div>').html(index+" - "+"MAC: "+MAC+
                                                                                        " Tipo: "+Type+
                                                                                        " Data: "+Date+
                                                                                        " Firmware: "+Firmware+
                                                                                        " Sala: "+room+
                                                                                        " Equipamento: "+equipment+
                                                                                        " Calibração: "+calibration+
                                                                                        " Próxima Calibração: "+next_calibration+
                                                                                        " RF: "+rf+
                                                                                        " Bateria: "+battery+"<b开发者_Python百科r>").appendTo('#page-wrap');
                });
            }
        });
    });
 </script>

Here is the code i use to display the entire document

and here is the xml file

< ?xml version="1.0" encoding="iso-8859-1"?>
<modulos>
<modulo>
    <MAC>foobar</MAC>
    <Type>foobar</Type>
    <Date>foobar</Date>
    <Firmware>foobar</Firmware>
    <room>foobar</room>
    <equipment>foobar</equipment>
    <calibration>foobar</calibration>
    <next_calibration>foobar</next_calibration>
    <rf>foobar</rf>
    <battery>foobar</battery>
</modulo>
<modulo>
    <MAC>foobar</MAC>
    <Type>foobar</Type>
    <Date>foobar</Date>
    <Firmware>foobar</Firmware>
    <room>foobar</room>
    <equipment>foobar</equipment>
    <calibration>foobar</calibration>
    <next_calibration>foobar</next_calibration>
    <rf>foobar</rf>
    <battery>foobar</battery>
    </modulo>
 </modulos>


create global object like this...

var myRecords ={
                 MAC: "",
                 Type: "",
                 Date: "",
                 Firmware: "",
                 room: "",
                 equipment: "",
                 calibration: "",
                 next_calibration: "",
                 rf: "",
                 battery: ""
          };

 myFunction (){

      $.ajax({
        type: "GET",
        url: "modulos.xml",
        dataType: "xml",
        success: function(xml) {
            $(xml).find('modulo').each(function(index){

                     myRecords.Mac[index] = $(this).find("Mac").text();
                     myRecords.Type[index]= $(this).find("Type").text();
                     myRecords.Date[index] = $(this).find("Date").text();
                     .
                     . and so on
            }
     }

so when you want to print all Mac in record then so say like

            for(var val in myRecords){
                       alert(myRecords[val].Mac);
             }

i hope this answers you

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜