How to check for an XML Node

Sue Frost
Giga Guru

I am getting some XML from a call to a mainframe program. Here's the XML that is returned:

<?xml version="1.0" encoding="UTF-8"?>

<soapenv:Envelope xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<soapenv:Body>

<NS1:Get_RateResponse xmlns:NS1="http://XXXX_Rating">

<quote>

        <contract>

                  <contractSysId>6f5ad58adb84c74032afdd0b5e9619dd</contractSysId>

                  <contractFullTermPremiumAmount>0</contractFullTermPremiumAmount>

        </contract>

        <coverageItem>

                  <cvgSeqNumIt>1</cvgSeqNumIt>

                  <cvgItSeqNum>1</cvgItSeqNum>

                  <cvgItSysId>d673349adbd4c70032afdd0b5e961919</cvgItSysId>

                  <bookRate>0.0000</bookRate>

                  <actualRate>0.0000</actualRate>

        <coverageItem>

        <cvgSeqNumIt>1</cvgSeqNumIt>

                  <cvgItSeqNum>2</cvgItSeqNum>

                  <cvgItSysId>6e045d28db2c83406b4f534e5e96192d</cvgItSysId>

                  <bookRate>0.0000</bookRate>

                  <actualRate>0.0000</actualRate>

        </coverageItem>

        <errorMessage>

                  <severity>2</severity>

                  <errorCode>0</errorCode>

                  <errorText>9899 Unratable due to missing data. Please check coverages and try again.</errorText>

                  <errorSysId>6f5ad58adb84c74032afdd0b5e9619dd</errorSysId>

                  <errorSysIdType>Contract</errorSysIdType>

        </errorMessage>

        <errorMessage>

                  <severity>2</severity>

                  <errorCode>0</errorCode>

                  <errorText>9444 Cannot find active rate version for coverage 0001</errorText>

                  <errorSysId>d673349adbd4c70032afdd0b5e961919</errorSysId>

                  <errorSysIdType>Coverage</errorSysIdType>

        </errorMessage>

</quote>

</NS1:Get_RateResponse>

</soapenv:Body>

I've got most of the code to write the results back to the correct records working. Now I'm looking at error handling.

It's entirely possible that there won't be any <errorMessage> nodes.

I can't figure out how to check if a node exists or not.

Here's the start of my code. In the IF statement on line 11, I've tried several different things

var xmlDocR = new XMLDocument2();

xmlDocR.parseXML(responseBody);

//stripping out all the 'update coverages' code

//Add FIRST error

node = xmlDocR.getFirstNode('//errorMessage');

gs.addInfoMessage("Point 1 - " + node);

if( node == '' ){     //doesn't work

//if( node.nil() ){     //doesn't work

        gs.addInfoMessage("Point 2 - no node");

} else {

        gs.addInfoMessage("Point 3 - found node");

}

The Point 1 info message reads:   Point 1 - NULL.

How do I test for a non-existent node?

1 ACCEPTED SOLUTION

Sue Frost
Giga Guru

After much moaning, groaning and gnashing of teeth, here's our final solution ... the If statement on line 3 does the trick.



//Initial Checks to see what is returned


contractNode = xmlDocR.getFirstNode('//contract');


if ( !contractNode ){


conNodeExists = 'no';


//gs.addInfoMessage("contractNode doesn't exist");


} else {


conNodeExists = 'yes';


//gs.addInfoMessage("contractNode exists ");



//Write out quote fields


var quoteSysID = xmlDocR.getNodeText("//contractSysId");


current.total_premium = xmlDocR.getNodeText("//contractFullTermPremiumAmount");


}


View solution in original post

6 REPLIES 6

Shishir Srivast
Mega Sage

Hi Sue,



Can you please try with



var node = xmldoc.getNodeText('//errorMessage');



Reference: XMLDocument Script Object - ServiceNow Wiki


I do have node declared at the beginning of my code. I did this:



var node2 = xmlDocR.getFirstNode('//errorMessage');


gs.addInfoMessage("Point 1 - " + node2);



if( node2 == '' ){


        gs.addInfoMessage("Point 2 - no node");


} else {


        gs.addInfoMessage("Point 3 - found node");


}



But with the same result - it's falling into the ELSE even though no <errorMessage> node exists.


Can we try like this, see if it helps.



var xmlDocR = new XMLDocument(responseBody, true);


var node = xmlDocR.getNodes("//errorMessage/*");  


gs.addInfoMessage("Point 1 - " + node);      


if( node == '' ){     //doesn't work  


//if( node.nil() ){     //doesn't work  


        gs.addInfoMessage("Point 2 - no node");  


} else {  


        gs.addInfoMessage("Point 3 - found node");  


}  


A copy & paste of this sample gives me an error:



org.mozilla.javascript.EcmaError: "XMLDocument" is not defined.