使用jQuery/JavaScript解析SOAP响应中的XML
我在使用jQuery解析来自SOAP服务器的响应时遇到问题。我想将XML响应转换为数组,因为有多行数据,如下所示 这是一项请求:使用jQuery/JavaScript解析SOAP响应中的XML,javascript,jquery,parsing,soap,Javascript,Jquery,Parsing,Soap,我在使用jQuery解析来自SOAP服务器的响应时遇到问题。我想将XML响应转换为数组,因为有多行数据,如下所示 这是一项请求: <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://sche
<?xml version="1.0" encoding="utf-8"?>
<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>
<GetWorkPos xmlns="http://localhost/apps">
<id>int</id>
</GetWorkPos>
</soap:Body>
</soap:Envelope>
int
以下是回应:
<?xml version="1.0" encoding="utf-8"?>
<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>
<GetWorkPosResponse xmlns="http://localhost.com/apps">
<GetWorkPosResult>
<GetWorkPos>
<ProductId>string</ProductId>
<Product>string</Product>
<quantity>decimal</quantity>
<Em>string</Em>
<type>string</type>
</GetWorkPos>
<GetWorkPos>
<ProductId>string</ProductId>
<Product>string</Product>
<Quantity>decimal</Quantity>
<Em>string</Em>
<Type>string</Type>
</GetWorkPos>
</GetWorkPosResult>
</GetWorkPosResponse>
</soap:Body>
</soap:Envelope>
一串
一串
十进制的
一串
一串
一串
一串
十进制的
一串
一串
这是我的代码:
$(document).ready(function () {
$("#send").click(function (event) {
var wsUrl = "http://localhost/Service.asmx";
var soapRequest =
'<?xml version="1.0" encoding="utf-8"?> \
<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> \
<GetWorkPos xmlns="http://localhost.com/apps"> \
<id>' + $("#id").val() + '</id> \
</GetWorkPos> \
</soap:Body> \
</soap:Envelope>';
console.log(soapRequest);
$.ajax({
type: "post",
url: wsUrl,
contentType: "text/xml",
dataType: "xml",
data: soapRequest,
success: processSuccess,
error: processError
});
});
});
function processSuccess(data, status, req, xml, xmlHttpRequest, responseXML) {
$(req.responseXML)
.find('GetWorkPosResult')
.each(function(){
var id = $(this).find('ProductId').text();
console.log(id);
});
}
function processError(data, status, req) {
alert(req.responseText + " " + status);
console.log(data);
console.log(status);
console.log(req);
}
$(文档).ready(函数(){
$(“#发送”)。单击(函数(事件){
var wsUrl=”http://localhost/Service.asmx";
var soapRequest=
' \
\
\
\
“+$(“#id”).val()+”\
\
\
';
日志(soapRequest);
$.ajax({
类型:“post”,
url:wsUrl,
contentType:“text/xml”,
数据类型:“xml”,
数据:soapRequest,
成功:成功,,
错误:processError
});
});
});
函数processSuccess(数据、状态、请求、xml、xmlHttpRequest、响应xml){
$(请求响应XML)
.find('GetWorkPosResult')
.each(函数({
var id=$(this.find('ProductId').text();
console.log(id);
});
}
功能处理错误(数据、状态、请求){
警报(请求响应文本+“”+状态);
控制台日志(数据);
控制台日志(状态);
控制台日志(req);
}
我无法将其转换为数组。只需将“id”值附加到数组中;您发布的代码没有尝试这样做。结果我得到的是100010011002,而不是10001001102。如果您不发布您试图用来创建数组的代码,没有人可以帮助您。我只是添加了var id=new array;。。。或者只是
myObj.push($(this.text())代码>-无需维护索引。谢谢…但现在我得到如下响应:因此一行中有5个变量。我也必须拆分它们,但是现在如果我输入console.log(x);仅显示最后一行中的值。如何选择要显示哪一个?console.log(myObj[0].text())
或console.log(myObj[0].find('ProductId').text())代码>如果您的问题得到回答,则针对您的其他问题开始另一个问题。
var myObj = new Array();
$(req.responseXML)
.find('GetWorkPosResult').find('GetWorkPos')
.each(function(){
myObj.push($(this)); // Should't use .text() because you'll lose the ability to use .find('tagName')
});
for(var i = 0; i<myObj.length;i++){
var x = myObj[i].find('ProductId').text();
var y = myObj[i]find('Product').text();
}
$(myObj).each(function(){
var x = $(this).find('ProductId').text();
var y = $(this).find('Product').text();
});