У меня проблемы с тем как добраться до нужного атрибута во время парсинга на node.js c xml
const axios = require('axios');
const parseString = require('xml2js').parseString;
const wsdlEndpoint = 'somelink';
const soapRequest = `
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:api="http://api.csr.mind.com" xmlns:open="http://api.csr.mind.com/openSession">
<soapenv:Header/>
<soapenv:Body>
<api:openSession>
<open:request>
<open:session key="12" />
<open:user name="name" password="password"/>
</open:request>
</api:openSession>
</soapenv:Body>
</soapenv:Envelope>
`;
axios.post(wsdlEndpoint, soapRequest, {
headers: {
'Content-Type': 'text/xml',
'SOAPAction': 'somelink',
},
})
.then(response => {
const soapResponse = response.data;
console.log('SOAP Response:', soapResponse);
parseString(soapResponse, { explicitArray: false, ignoreAttrs: true }, (err, result) => {
if (err) {
console.error('Error parsing SOAP response:', err);
} else {
const sessionID = result['soapenv:Envelope']['soapenv:Body']['ns1:openSessionReturn']['ns88:response']['ns88:session'][0]['$']['id'];
console.log('Session ID:', sessionID);
}
});
})
.catch(error => {
console.error('Error making SOAP request:', error);
});
Вот сам ответ, почему то не могу взять этот id <ns88:session id="123456789"/>
<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:openSessionReturn xmlns:ns1="http://api.com">
<ns88:response xsi:noNamespaceSchemaLocation="open.xsd"
xmlns:ns88="http://api.csr.mind.com/openSession">
<ns88:session id="1776470983"/>
</ns88:response>
</ns1:openSessionReturn>
</soapenv:Body>
</soapenv:Envelope>
Необходимо мне получить этот атрибут, до него могу добраться, а именно ID Не могу получить.
Ответы (1 шт):
Автор решения: ksa
→ Ссылка
не могу взять этот id
<ns88:session id="123456789"/>
Вот так его можно получить...
const xmlString = `<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:openSessionReturn xmlns:ns1="http://api.com">
<ns88:response xsi:noNamespaceSchemaLocation="open.xsd"
xmlns:ns88="http://api.csr.mind.com/openSession">
<ns88:session id="1776470983"/>
</ns88:response>
</ns1:openSessionReturn>
</soapenv:Body>
</soapenv:Envelope>`;
const domParser = new DOMParser();
const xmlDOM = domParser.parseFromString(xmlString, "text/xml");
const o = xmlDOM.querySelector("[id]");
console.log(o.id);