1.下载xmldom库:https://github.com/jindw/xmldom
2.新建一个lib文件夹,将dom-parser.js、dom.js、entities.js、sax.js拷贝到lib文件夹下
3.在需要解析xml的js头部引入dom-parser.js,如:var Parser = require(‘../../../lib/dom-parser’)
xml
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:getMaterialsResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:ns1="http://service.ws.*.com/">
<getMaterialsReturn xsi:type="xsd:string">[{"name":"GL201","number":"1202001"}]</getMaterialsReturn>
</ns1:getMaterialsResponse>
</soapenv:Body>
</soapenv:Envelope>
/**输入搜索框,请求后端数据 */
searchMterialList: function() {
var method = 'getMaterials';
//wsdlurl中设置需要访问的webservice的url地址
var wsdlurl = 'http://127.0.0.1:56898/ormrpc/services/WSLyFacade?wsdl';
//var targetNamespace = 'http://service.ws.*.com/'; 这个也可以用
var targetNamespace = 'http://127.0.0.1:56898/ormrpc/services/WSLyFacade';
//datacopy中拼字符串,即http传输中的soap信息
var datacopy = '<?xml version="1.0" encoding="utf-8"?>';
datacopy += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.ws.*.com/">';
datacopy += '<soapenv:Header/>';
datacopy += '<soapenv:Body>';
//接着拼你要访问的方法名、参数名和你传递的实参值,比如我要访问的方法是getReader(String arg0,int arg1)
//而我的实际调用是getReader('libsys',2),所以拼字符串如下
datacopy += '<ser:getMaterials>'; //方法名
datacopy += '<ser:arg0>G</ser:arg0>';//参数
datacopy += '</ser:getMaterials>';
datacopy += '</soapenv:Body>';
datacopy += '</soapenv:Envelope>';
wx.request({
url: wsdlurl,
data: datacopy,
method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: {
'content-type': 'text/xml; charset=utf-8',
'SOAPAction': targetNamespace + method,
},
success(res) {
var XMLParser = new Parser.DOMParser() //小程序导入dom解析插件
var doc = XMLParser.parseFromString(res.data)
var a = doc.getElementsByTagName('getMaterialsReturn')['0'].childNodes[0].nodeValue;
//var b = doc.getElementsByTagName('name')['0'];
//console.log(res.data);
// console.log(doc);
console.log(a);
//console.log(b);
}
})
}
得到json:
[{“name”:”GL201″,”number”:”1202001″}]
版权声明:本文为MZ199290原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。