0

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);
});
8
  • 1
    are you trying to parse the response from client side or via terminal independently ? Commented Apr 29, 2020 at 12:21
  • @TheoNeUpKID - I created a API to get the result from web browser. Now I want to return only basekey with that API. Commented Apr 29, 2020 at 12:24
  • 1
    You need to select one of the approaches you tried. Show us exactly what you did, and tell us exactly how it failed, and then we can tell you what you did wrong. Telling us you tried everything, nothing worked, and can we suggest anything else is not a good way of approaching problem solving. (We need to see your mistakes. For example, perhaps you just haven't grokked namespaces.) Commented Apr 29, 2020 at 12:29
  • @MichaelKay - I updated the question. Can I Fix it ?? Commented Apr 29, 2020 at 12:41
  • 1
    The conventional way would be to build an XML DOM and navigate the DOM. Commented Apr 30, 2020 at 6:24

3 Answers 3

1

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'
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

4 Comments

I tried with this res.end(json_res['soap-env:Envelope']);. But, nothing happened. No response or errors. Then I added another one like this res.end(json_res['soap-env:Envelope']['soap-env:Header']); And I got this error - TypeError: Cannot read property 'soap-env:Header' of undefined. What can I do ??
is var parseStringReq initialized
yes, var parseStringReq = require('xml2js'); What is the problem ??
It’s wasn’t included in your code sample and wanted to make sure you initialized the variable.
0

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"]); 
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.