Amithash
Amithash

Reputation: 277

How to read attributes and values in XML using Node Js

I used a SOAP API to get the response. And now I have a long XML response. I tried to convert XML into JSON and then to read. But, it couldn't work. Then I tried xml2js , xmldom and filterxml to read the attributes. But, it didn't work.

XML Response.

<?xml version="1.0" encoding="UTF-8"?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
    <soap-env:Header>
        <eb:MessageHeader xmlns:eb="http://www.ebxml.org/namespaces/messageHeader" eb:version="1.0" soap-env:mustUnderstand="1">
            <eb:From>
                <eb:PartyId eb:type="URI" />
            </eb:From>
            <eb:To>
                <eb:PartyId eb:type="URI" />
            </eb:To>
            <eb:CPAId>36465</eb:CPAId>
            <eb:ConversationId>4767547547745757</eb:ConversationId>
            <eb:Service eb:type="sabreXML">Session</eb:Service>
            <eb:Action>SessionCreateRS</eb:Action>
            <eb:MessageData>
                <eb:MessageId>45757457547</eb:MessageId>
                <eb:Timestamp>2020-04-29T12:13:28</eb:Timestamp>
                <eb:RefToMessageId>63465653634ffghfghfghf</eb:RefToMessageId>
            </eb:MessageData>
        </eb:MessageHeader>
        <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext">
            <wsse:BinarySecurityToken valueType="String" EncodingType="wsse:Base64Binary">bmkfdkdgkdskmlskfsdfpskmfl</wsse:BinarySecurityToken>
        </wsse:Security>
    </soap-env:Header>
    <soap-env:Body>
        <SessionCreateRS xmlns="http://www.opentravel.org/OTA/2002/11" version="1" status="Approved">
            <ConversationId>4767547547745757</ConversationId>
        </SessionCreateRS>
    </soap-env:Body>
</soap-env:Envelope>

**Update

As I told I tried to convert XML into JSON using xml2jsn. And it gave me the complete json response. But, I can't go for a attribute that I want because of these symbols, -(dash) and :(colon).

I just want to get " wsse:BinarySecurityToken " value. Then how can go for the wsse:BinarySecurityToken ??

var parseString = parseStringReq.parseString;

parseString(SetSoapTokenRes, function (err, result) {
    let json_res = JSON.stringify(result, null, 4);
    // json_res = json_res.replace(':', '_') //output: 'A D C'
res.end(json_res.soap-env:Envelope);
});

Upvotes: 0

Views: 1394

Answers (3)

Tuan Anh Tran
Tuan Anh Tran

Reputation: 7237

Alternatively, if you want to make it simpler by transforming XML to JSOn with only the data you are interested in, you can use camaro and do this:

const { ready, transform } = require('camaro')

const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
    <soap-env:Header>
        <eb:MessageHeader xmlns:eb="http://www.ebxml.org/namespaces/messageHeader" eb:version="1.0" soap-env:mustUnderstand="1">
            <eb:From>
                <eb:PartyId eb:type="URI" />
            </eb:From>
            <eb:To>
                <eb:PartyId eb:type="URI" />
            </eb:To>
            <eb:CPAId>36465</eb:CPAId>
            <eb:ConversationId>4767547547745757</eb:ConversationId>
            <eb:Service eb:type="sabreXML">Session</eb:Service>
            <eb:Action>SessionCreateRS</eb:Action>
            <eb:MessageData>
                <eb:MessageId>45757457547</eb:MessageId>
                <eb:Timestamp>2020-04-29T12:13:28</eb:Timestamp>
                <eb:RefToMessageId>63465653634ffghfghfghf</eb:RefToMessageId>
            </eb:MessageData>
        </eb:MessageHeader>
        <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext">
            <wsse:BinarySecurityToken valueType="String" EncodingType="wsse:Base64Binary">bmkfdkdgkdskmlskfsdfpskmfl</wsse:BinarySecurityToken>
        </wsse:Security>
    </soap-env:Header>
    <soap-env:Body>
        <SessionCreateRS xmlns="http://www.opentravel.org/OTA/2002/11" version="1" status="Approved">
            <ConversationId>4767547547745757</ConversationId>
        </SessionCreateRS>
    </soap-env:Body>
</soap-env:Envelope>
`
const template = {
    token: 'soap-env:Envelope/soap-env:Header/wsse:Security/wsse:BinarySecurityToken',
    conversationId: 'soap-env:Envelope/soap-env:Body/SessionCreateRS/ConversationId'
}

;(async function () {
    const result = await transform(xml, template)
    console.log(result)
})()

the template is just an example. you can add more fields if you want. Accessing token is simply result.token

example output

{
  token: 'bmkfdkdgkdskmlskfsdfpskmfl',
  conversationId: '4767547547745757'
}

Upvotes: 1

Amithash
Amithash

Reputation: 277

Finally, I converted XML into JSON using xml2js package. And used below code to read attributes as I want.

var xml2js = require('xml2js');
var parser = new xml2js.Parser();

parser.parseString(xml_response, function (err, result) {
    if (err) {
        console.error('here is the eror: ', err);
    } else {
        console.log(JSON.stringify(result, null, 2));
        console.log(["soap-env:Envelope"]["soap-env:Header"][0]["token"]); 
    }
});

Upvotes: 0

TheoNeUpKID
TheoNeUpKID

Reputation: 893

looks like your trying to get the result incorrectly. Instead of json_res.soap-env:Envelope try json_res['soap-env:Envelope'] it maybe escaping because of the special character.

Upvotes: 1

Related Questions