How to differentiate XML parsing

Supriya25
Tera Guru

Hi All, 

 

Requesting help : How to handle If OUTPUT tag has no Obj or Array then please help me how to parse it.
var _strXML = '<results probe_time="1633" result_code="0"> <result> <output xmls:xsi="https/test"  xsi:nil-"true></output> </result></results>';

 

 

 

If OUTPUT tag has Obj or Arr there we don't have any issue , getting proper result 
var _strXML = '<results probe_time="1633" result_code="0"> <result> <output> { "seccode":"12345" , "secstatus":"DRILL" , "secid":"654", "errorDetails":[ {"code" :"4001", "description":"auth fail"} ] }</output> </result></results>';

 var _objXml = new global.XMLDocument(_strXML);
    var _arrResults = JSON.parse(_objXml.getNodeText('//output').trim());

    var seccode = _arrResults.seccode;
    gs.info("seccode :" + seccode);
    var detailsArray = _arrResults.errorDetails;
    for (i = 0; i < detailsArray.length; i++) {
        gs.info("Code: " + detailsArray[i].code +
            "\nDescription: " + detailsArray[i].description);
    }

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @Supriya25 ,

You can add a check to see if the content of the <output> tag is empty or not before attempting to parse it as JSON.

 

var _strXML = '<results probe_time="1633" result_code="0"> <result> <output xmls:xsi="https/test"  xsi:nil="true"></output> </result></results>';

var _objXml = new global.XMLDocument(_strXML);
var outputText = _objXml.getNodeText('//output').trim();

// Check if the <output> tag is empty or null
if (outputText === "" || outputText === "null") {
    gs.info("Output tag is empty or null");
} else {
    try {
        var _arrResults = JSON.parse(outputText);

        var seccode = _arrResults.seccode;
        gs.info("seccode :" + seccode);
        var detailsArray = _arrResults.errorDetails;
        for (var i = 0; i < detailsArray.length; i++) {
            gs.info("Code: " + detailsArray[i].code +
                "\nDescription: " + detailsArray[i].description);
        }
    } catch (e) {
        gs.error("Error parsing JSON from output tag: " + e.message);
    }
}

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

 

View solution in original post

3 REPLIES 3

Community Alums
Not applicable

Hi @Supriya25 ,

You can add a check to see if the content of the <output> tag is empty or not before attempting to parse it as JSON.

 

var _strXML = '<results probe_time="1633" result_code="0"> <result> <output xmls:xsi="https/test"  xsi:nil="true"></output> </result></results>';

var _objXml = new global.XMLDocument(_strXML);
var outputText = _objXml.getNodeText('//output').trim();

// Check if the <output> tag is empty or null
if (outputText === "" || outputText === "null") {
    gs.info("Output tag is empty or null");
} else {
    try {
        var _arrResults = JSON.parse(outputText);

        var seccode = _arrResults.seccode;
        gs.info("seccode :" + seccode);
        var detailsArray = _arrResults.errorDetails;
        for (var i = 0; i < detailsArray.length; i++) {
            gs.info("Code: " + detailsArray[i].code +
                "\nDescription: " + detailsArray[i].description);
        }
    } catch (e) {
        gs.error("Error parsing JSON from output tag: " + e.message);
    }
}

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

 

Hi Sanjay, Yes it is working as expected. Thanks for grate favor.

some times XML output coming like below

var _strXML = '<results probe_time="1633" result_code="0"> <result> <output xmls:xsi="https/test"  xsi:nil-"true></output> </result></results>';
 Or

var _strXML = '<results error="Method failed 401 probe_time="1633" result_code="0"> <result error="Method failed 401"> <output>{"result" : "FATAL","errorDtails":[{....}]</output> </result></results>';

I tried below method , is it right process which I tried.???

var _objXml = new global.XMLDocument(_strXML);
var outputText = _objXml.getNodeText('//output/@errorDetails').trim();

Or 

var _objXml = new global.XMLDocument(_strXML);
var outputText = _objXml.getNodeText('//results/@error').trim();

 

Community Alums
Not applicable

Hi @Supriya25 ,

 

I think you need to check for valid data as well apart from only checking for NULL, so you need to take a look in the response in output tag that you and identifies for the valid JSON, might be some attribute or a key in the JSON.

You can put a condition check like if output contains "desired key/value" and is not NULL then consider it as valid data.
Hope this helps.


Thanks & Regards,

Sanjay Kumar