Validating whether a string is well-formed XML

dclayne
Kilo Expert

I'm trying to come up with a script for a scoped application that will validate whether the content of a string field is well-formed XML. I was thinking I could call the XMLDocument2 object wrapper and use a try/catch scenario to tell me whether the parseXML function worked on the provided XML string. For some reason, the error encountered by badly-formed XML against the XMLDocumentXML2 object cannot be caught within JavaScript.

Script Version 1:

//Good XML string

try{

var xmlString = "<test><one><two att=\"xxx\">abcd1234</two><three boo=\"yah\" att=\"yyy\">1234abcd</three><two>another</two></one><number>1234</number></test>";

var xmlDoc = new XMLDocument2();

xmlDoc.parseXML(xmlString);

gs.info(xmlDoc.toString());

}

catch(err){

gs.info('bad');

}

Script Version 2:

//Bad XML string with <one> missing

try{

var xmlString = "<test><two att=\"xxx\">abcd1234</two><three boo=\"yah\" att=\"yyy\">1234abcd</three><two>another</two></one><number>1234</number></test>";

var xmlDoc = new XMLDocument2();

xmlDoc.parseXML(xmlString);

gs.info(xmlDoc.toString());

}

catch(err){

gs.info('bad');

}

Running the second script in the background script utility doesn't actually capture the error that is encountered when I use parseXML against the badly-formed XML string. Has anyone ever needed to validate XML in a ServiceNow form that's part of a scoped application? Since there is no XML field datatype, and since I'm trying to accomplish this in a scoped application, does anyone have any alternatives for validating whether an XML string is well-formed?

4 REPLIES 4

Troy Riblett
Giga Guru

This is a bit of a hack, but I think something like this may work.



try{


      var xmlString = "<test><two att=\"xxx\">abcd1234</two><three boo=\"yah\" att=\"yyy\">1234abcd</three><two>another</two></one><number>1234</number></test>";


var xmlDoc = new XMLDocument2();


     


      xmlDoc.parseXML(xmlString);


      if(xmlDoc.toString() == "" && xmlString != ""){


              gs.info('also bad');


      } else{


                  gs.info('good');


        }


}


catch(err){


      gs.info('bad');


}



It looks like if the parse fails that .toString() will return an empty string. The exception is still going to be put in the logs however..


Thanks, Troy. I wasn't aware the error would always post to the logs. This explains the issue I am seeing. ServiceNow apparently handles the exception in the back-end, so when the XMLDocument2 parsing fails (gs.info(xmlDoc.parseXML(xmlString).toString()), it doesn't trigger the catch scenario in a handled exception. This makes sense now.



It seems like there should be a better approach to determining whether a document is well-formed XML, though, especially since the error cannot be suppressed/handled in the calling script.


EashVerma
Giga Expert

Hello David,



I tried running your script and apparently, it is working fine in my system. I am using Jakarta instance.



Capture.PNG



Let me know if you continue to face this issue.



Regards,



Eash Verma


dclayne
Kilo Expert

It looks like we can use xmlDoc.isValid(xmlString) to validate whether the field comprises well-formed XML before actually parsing it. Yay!