Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

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


}


Hi,

Is it possible to update xmlstring specific tag value. I have tried below to get, but i need to update, is 

 

<?xml version="1.0" encoding="UTF-8"?>
<webGroups class="com.linoma.dpa.commandcenter.codec.WebGroupVOListCodec">
<webGroup>
<inviteUserPermission>true</inviteUserPermission> 
<secureMailPermissions>1</secureMailPermissions>
<webUsers>
<webUser>
<name>RGKrish183@gmail.com</name> // i need to update value #Gopal@gm.com
</webUser>
</webUsers>
</webGroup>
</webGroups>

var xmlString = //above xml values from GlideRecord
var xmldoc = new XMLDocument(xmlString);
var node = xmldoc.getNodeText("/webGroups/webGroup/webUsers/webUser/name");
gs.print(node); // now getting RGKrish183@gmail.com

 

 

Thanks, 

Gopal R