如何使用Jquery解析多个XML数据?

时间:2012-07-26 11:12:30

标签: javascript jquery xml jquery-mobile xml-parsing

我正在使用Ajax调用获取XML响应,我可以解析数据但只有一个数据而不是多个数据元素,我在xml响应中得到的是:

           <GetTimeSlotsForUserType_1Response>
             <GetTimeSlotsForUserType_1Result>
              <NewDataSet xmlns="">
              <Table diffgr:id="Table1" msdata:rowOrder="0">
                 <SlotId>406</SlotId>
                 <TimeAvailable> 01:00</TimeAvailable>
                 <CutOffHours>0.50</CutOffHours>
                 <Sequence>1</Sequence>
              </Table>
              <Table diffgr:id="Table2" msdata:rowOrder="1">
                 <SlotId>408</SlotId>
                 <TimeAvailable>02:00</TimeAvailable>
                 <CutOffHours>0.50</CutOffHours>
                 <Sequence>1</Sequence>
              </Table>
              <Table diffgr:id="Table3" msdata:rowOrder="2">
                 <SlotId>410</SlotId>
                 <TimeAvailable>03:00</TimeAvailable>
                 <CutOffHours>0.50</CutOffHours>
                 <Sequence>1</Sequence>
              </Table>
              <Table diffgr:id="Table4" msdata:rowOrder="3">
                 <SlotId>412</SlotId>
                 <TimeAvailable>04:00</TimeAvailable>
                 <CutOffHours>0.50</CutOffHours>
                 <Sequence>1</Sequence>
              </Table>
              <Table diffgr:id="Table5" msdata:rowOrder="4">
                 <SlotId>414</SlotId>
                 <TimeAvailable>05:00</TimeAvailable>
                 <CutOffHours>0.50</CutOffHours>
                 <Sequence>1</Sequence>
              </Table>
           </NewDataSet>
     </GetTimeSlotsForUserType_1Result>
  </GetTimeSlotsForUserType_1Response>

我需要各种元素并将它们逐一放入HTML页面的“选择”菜单中。

Jquery代码:

function GetTimeSlotsForUser(){
var soapMessage4='<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><GetTimeSlotsForUserType_1 xmlns="http://there.org/"><opFacId>'+ OpFacId +'</opFacId><requestType>'+ check +'</requestType><category>'+ category +'</category><availableFor>'+ issignee +'</availableFor></GetTimeSlotsForUserType_1></soap:Body></soap:Envelope>';

 $.ajax({
    url: "http://33.204.22.31/therewebservice/therewebservice.asmx?op=GetTimeSlotsForUserType_1",
    type: "POST",
    dataType: "xml",
    SOAPAction: "http://there.org/GetTimeSlotsForUserType_1",
    data: soapMessage4,
    complete: endSaveProduct4,
    contentType: "text/xml; charset=\"utf-8\""
       });

return false;
}

function endSaveProduct4(xmlHttpRequest,status){
var optionlist='<option value="select-value-2">-- Select Time --</option>';

$(xmlHttpRequest.responseXML)
.find('NewDataSet')
.each(function()
      {
      var timeAvailable=$(this).find('TimeAvailable').text();
      optionlist += '<option>' + timeAvailable + '</option>';

      });
$("#select-choice-2").html(optionlist).selectmenu('refresh', true);
}

直到这里我能够获得选项列表中的数据,但它只是获取所有元素并将其作为一个选项放在我的选择菜单中,如下所示......:

You can see the data is coming in one single option, instead of different options

我想要的是解析xml中所有不同的“Time Available”元素,并在选择菜单中将其显示为多个选项。 请帮忙。

2 个答案:

答案 0 :(得分:1)

这应该有效

timeSlots = $(xmlHttpRequest.responseXML).find('TimeAvailable')
$(timeSlots).each(function(){ 
    optionlist += '<option>' + $(this).text() + '</option>';
});

答案 1 :(得分:0)

使用OpenJS将XML解析为js对象。接下来的步骤很简单。