I am new to node.js
and need to parse XML
. In js, I just use a DOMParser
and use getElementsByTagName
to get the values I desire. I have just switched to node and am using xml2js
(willing to consider alternatives). I have not been able to figure out how to parse through the response to get what I am looking for.
Code below:
function parseBody(body){
var parseString = require('xml2js').parseString;
var xml = body
parseString(xml, function (err, result) {
console.dir(result);
});
and here is the XML I am passing into the function:
<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:int=\"realURLHere">
<soapenv:Header/>
<soapenv:Body>
<int:readResponse>
<response>
<request>?</request>
<cust>
<fName>?</fName>
</cust>
</response>
</int:readResponse>
</soapenv:Body>
</soapenv:Envelope>
Here is the output of my function:
{ 'soapenv:Envelope':
{ '$':
{ 'xmlns:soapenv': 'http://schemas.xmlsoap.org/soap/envelope/',
'xmlns:int': 'realURLHere' },
'soapenv:Body': [ [Object] ]
}
}
I am trying to access the value of <fName>
within <cust>
but having no luck. Any help would be appreciated. Thanks!