当我在html页面中调用rest服务时,得到响应xml。
响应xml看起来像:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<sys:systems xmlns:sys="http://tempuri.org">
    <system>
        <ID>System1</ID>
        <ScheduledStartDateTime>2013-06-12T00:00:00</ScheduledStartDateTime>
        <ScheduledEndDateTime></ScheduledEndDateTime>
        <operations>
            <operation>
                <OperationID>0010</OperationID>
                <Status>Running</Status>
                <ID>10003814</ID>
            </operation>
        </operations>
    </system>
</sys:systems>


然后,我尝试使用jQuery解析上述响应,为此,我正在执行以下操作:

$(xmlDoc).find('sys\\:systems\\system\\ID,ID').text();


在执行上述声明的警报后,我获得了ID值,但我同时获得了两个ID值,而不是仅获得一个ID值。

所以我想获取的是ID = System1,但是我同时获得了System1和10003814。

我如何只获取System1作为ID?期待您的解决方案。
提前致谢。

最佳答案

您可以直接使用后代选择器定位所需的ID节点。尝试这个:

var id = $(xmlDoc).find('system > ID').text();


工作示例:



var xmlDoc = '<?xml version="1.0" encoding="UTF-8" standalone="no"?><sys:systems xmlns:sys="http://tempuri.org"><system><ID>System1</ID><ScheduledStartDateTime>2013-06-12T00:00:00</ScheduledStartDateTime><ScheduledEndDateTime></ScheduledEndDateTime><operations><operation><OperationID>0010</OperationID><Status>Running</Status><ID>10003814</ID></operation></operations></system></sys:systems>'

var id = $(xmlDoc).find('system > ID').text();
alert(id);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

09-30 17:23
查看更多