简体   繁体   English

javascript,如何从XML获取价值

[英]javascript, how to get a value from XML

I'm learning how to use javascript to call web method from ASMX service using XMLHttpRequest class. 我正在学习如何使用JavaScript通过XMLHttpRequest类从ASMX服务调用Web方法。 I've managed to write the following: 我设法写了以下内容:

function GetDataService() {
            if (window.XMLHttpRequest) {
                xmlHTTP = new window.XMLHttpRequest;
            } else {
                alert("Wrong!");
            }

            xmlHTTP.open("POST", "http://localhost:45250/ServiceJava.asmx", true);
            xmlHTTP.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
            xmlHTTP.setRequestHeader("SOAPAction", "http://localhost:45250/ServiceJava.asmx/GetTimeString");

            strRequest = '<?xml version="1.0" encoding="utf-8"?>';
            strRequest = strRequest + '<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/">';
            strRequest = strRequest + '<soap:Body>';
            strRequest = strRequest + '<GetTimeString xmlns="http://localhost:45250/ServiceJava.asmx" />';
            strRequest = strRequest + '</soap:Body>';
            strRequest = strRequest + '</soap:Envelope>';

            //Different value for readystate
            //0--Uninitialized
            //1--Loading
            //2--loaded(but data not recieved)
            //3--Interactive--Some part of the data is recieved
            //4--Completed(all data recieved)
            xmlHTTP.onreadystatechange = function () {
                if (xmlHTTP.readyState == 4 && xmlHTTP.status == 200) {
                    var x = xmlHTTP.responseXML;
                    document.getElementById("time").textContent = x;                  
                }
            }
            xmlHTTP.send(strRequest);
        }

But it produces the code: 但是它产生代码:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetTimeStringResponse xmlns="http://localhost:45250/ServiceJava.asmx"><GetTimeStringResult>14:31:28</GetTimeStringResult></GetTimeStringResponse></soap:Body></soap:Envelope>

Now I would like to get only the 14:31:28 . 现在我只想得到14:31:28 How can I do that? 我怎样才能做到这一点? I've tried to find the answer but x doesn't seem to have method like getElementByTagName() or anything similiar. 我试图找到答案,但x似乎没有getElementByTagName()类的方法或类似的方法。

Thanks! 谢谢!

Solution without using jQuery 不使用jQuery的解决方案

var xmlStr = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope ...';    
var xml = new window.DOMParser().parseFromString(xmlStr, "text/xml");   
var value = xml.getElementsByTagName("GetTimeStringResult")[0].innerHTML;

You can use for example the jQuery parseXML method. 您可以使用例如jQuery parseXML方法。

var response = "<?xml version="1.0" encodi...";
var xml= $($.parseXML(response));
var value = xml.find("GetTimeStringResult").text();

However, if you choose to send the request with jQuery instead of vanilla javascript, you get the response already deserialized in the callback. 但是,如果您选择使用jQuery而不是普通的javascript发送请求,则会在回调中获得已经反序列化的响应。

I recommend that you don't write the XMLHttpRequests by hand. 我建议您不要手工编写XMLHttpRequests。 Instead use jQuery.ajax(): 而是使用jQuery.ajax():

$.ajax({
    url: "http://localhost:45250/ServiceJava.asmx/GetTimeString",
    type: "POST",
    data:  postData,
    success: function(data, textStatus, jqXhr) {
             var result = data.GetTimeStringResult;
    }
});

'postData' should be a JSON object in which you pass your parameters to your endpoint. “ postData”应该是一个JSON对象,您可以在其中将参数传递给端点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM