【问题标题】:How to parse multiple XML data using Jquery?如何使用 Jquery 解析多个 XML 数据?
【发布时间】:2012-07-26 11:12:30
【问题描述】:

我正在使用 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);
}

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

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

【问题讨论】:

  • 如果您认为某个回复回答了您的问号,请将其标记为正确的

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


【解决方案1】:

这应该可以工作

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

【讨论】:

  • 我能够解析 XML,但只能在单个数据元素中解析,即 timeAvailable,但正如您所见,有很多 timeAvailable 元素,这就是我所需要的。我的代码将它们全部解析在一行中,我需要一些数组或其他东西来存储不同的 timeAvailable 元素并在选择菜单中显示它们。
【解决方案2】:

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

【讨论】:

  • 我能够解析 XML,但只能在单个数据元素中解析,即 timeAvailable,但正如您所见,有很多 timeAvailable 元素,这就是我所需要的。我的代码将它们全部解析在一行中,我需要一些数组或其他东西来存储不同的 timeAvailable 元素并在选择菜单中显示它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-24
  • 1970-01-01
  • 1970-01-01
  • 2013-06-09
  • 2020-11-28
  • 1970-01-01
相关资源
最近更新 更多