Reputation: 649
I have just retrieved an object using SOAP and HTTPClient from Angular5 and i'm having issues to retrieve specific values. The thing is, I can access only all of them using:
import {parseString} from 'xml2js';
...
parseString(data, function (err, result) {
var stringified = JSON.stringify(result);
console.log(stringified);
}
For example below is the XML converted to JSON that I have. I have two questions:
1) How would I retrieve the specific << values >> inside of it? Like id, name, event ... InSpec, OutSpec, MiddleSpec and else?
{
"soap:Envelope": {
"$": {
"xmlns:soap": "http://www.w3.org/2003/05/soap-envelope",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xmlns:xsd": "http://www.w3.org/2001/XMLSchema"
},
"soap:Body": [{
"List": [{
"$": {
"xmlns": "http://WebService/"
},
"ListResult": [{
"Total": ["1"],
"Dados": [{
"Posicao": [{
"id": ["<<value>>"],
"name": ["<<value>>"],
"event": ["<<value>>"],
"address": ["<<value>>"],
"description": ["<<value>>"],
"addressid": ["<<value>>"],
"number": ["<<value>>"],
"city": ["<<value>>"],
"descriptionEvent": ["<<value>>"],
"lat": ["<<value>>"],
"long": ["<<value>>"],
"In": [{
"InSpec": [{
"Descricao": ["<<value>>"],
"Valor": ["false"]
}, {
"Descricao": ["<<value>>"],
"Valor": ["false"]
}, {
"Descricao": ["<<value>>"],
"Valor": ["false"]
}, {
"Descricao": ["<<value>>"],
"Valor": ["false"]
}, {
"Descricao": ["<<value>>"],
"Valor": ["true"]
}, {
"Descricao": ["<<value>>"],
"Valor": ["false"]
}, {
"Descricao": ["<<value>>"],
"Valor": ["false"]
}]
}],
"Out": [{
"OutSpec": [{
"Descricao": ["<<value>>"],
"Valor": ["false"]
}, {
"Descricao": ["<<value>>"],
"Valor": ["false"]
}]
}],
"Middle": [{
"MiddleSpec": [{
"Descricao": ["<<value>>"],
"Valor": ["506"]
}, {
"Descricao": ["<<value>>"],
"Valor": ["0"]
}, {
"Descricao": ["<<value>>"],
"Valor": ["0"]
}, {
"Descricao": ["<<value>>"],
"Valor": ["8"]
}, {
"Descricao": ["<<value>>"],
"Valor": ["0"]
}, {
"Descricao": ["<<value>>"],
"Valor": ["12.54"]
}, {
"Descricao": ["<<value>>"],
"Valor": ["4658"]
}, {
"Descricao": ["<<value>>"],
"Valor": ["4.1"]
}]
}]
}]
}]
}]
}]
}]
}
}
2) Is it better to convert from XML to JSON to retrieve these values or is it possible to get them directly from XML?
Appreciate the help.
Upvotes: 0
Views: 519
Reputation: 324
Assuming that you have a stringified json. This works
let jsonObject = JSON.parse(stringified);
console.log('jsonObject', jsonObject);
let jsonEnvelope = jsonObject['soap:Envelope'];
let jsonBody = jsonEnvelope['soap:Body'];
console.log(jsonBody);
let jsonList = jsonBody[0];
console.log(jsonList);
let jsonListResult = jsonList['List'];
console.log(jsonListResult);
Of course a proper answer would be to create a mapper and proper models for your soap response.
Upvotes: 1