logo

NJP

Discovery: Parsing retrieved XML files

Import · Sep 15, 2011 · article

Recently, a customer asked me if it is possible to write a probe and sensor to get an XML file from a Windows target. Getting the file was the easy part. Parsing the returned results in the sensor was a new learning experience for me. Since I couldn't seem to find it documented anywhere else, this seemed like the best place. That's right... from the developer, to me, to you in just a few hours.Retrieving the XML file is the same as with any other text file. Create a probe under Discovery Definition> Probes, and click New. Define the probe like this:Name: Windows - Get XML fileProbe type: ProbeECC queue topic: RemoteRunnerECC queue name: WMI: Get XML fileDescription: Retrieve an XML file for some applicationUsed by Discovery: trueUsed by Runbook: falseRight click, SaveUnder the "Probe Parameters" (related list), create a new entry with the following parameters:Name: WMI_get_xml_file.jsProbe: (reference to the parent probe record: Windows - Get XML file)Active: trueValue:

//// Sample probe to gather text from the file: D:\myapp\config\database.xml//var FOR_READING = 1;var OUTPUT_FILE = "D:\myapp\config\database.xml";var res = getInfo();WScript.Echo(res);function getInfo() { var output = readFile(OUTPUT_FILE); return output;}function readFile(fileName) { var fileObj = new ActiveXObject("Scripting.FileSystemObject"); var objTextFile = fileObj.OpenTextFile(fileName, FOR_READING); var output = objTextFile.ReadAll(); objTextFile.close(); return output;}

Value script: (empty)Now comes the sensor. Normally, we would create a variable as an XMLdocument object from the result.output and use getNodeText() to tease out the parameters we want. That's not necessary here (never mind the fact that it doesn't work either). Discovery is smart enough to recognize that we retrieved an XML file and puts it directly inline with the XML payload on the ECC input record (look under the tag).

Here's the cool part I learned... To get to that information, simply dot-walk the XML structure to the answer. Given the following information in the

section of the XML payload...

abcd1234 1234abcd another 1234

Instead of using XMLDocument and getNodeText() functions, use the much simpler statement:

var myNum = result.test.number;

To use this in a sensor, create a new sensor from Discovery Definition> Sensors, click New and fill in the form similar to the following:Name: Windows - Get XML fileReacts to probe: Windows - Get XML fileSensor type: SensorDescription: Tell me what's in the "number" tag of the retrieved XML fileSensor type: JavascriptActive: trueScript:

//// Name: My sensor// Description: Parse the output retrieved by the probe of the same name//new DiscoverySensor({/** Process the results of the probe** @param: result - the value of the "result" tag of the XML payload on the input record*/process: function(result) { var myNum = result.test.number; // Dot walk to the answer gs.log('>>>DEBUG: My sensor: number=' + myNum);}, type: "DiscoverySensor"});

The power and simplicity of this is the statement "var myNum=result.test.number" to dot-walk to the answer. If you look in the system log, you should see an entry with the message:

DEBUG: My sensor: number=1234

I had one challenge when I ran in to with XML tags that have dashes (-) in them. The JavaScript parser thinks of these as mathematical equations with subtraction. For example:

var myNum = result.data-sources.data-source.name; // Won't work !!!

is interpreted as "

result.data

" minus "

sources.data

" minus "

source.name

". I got an error stating "sources is an unknown reference". The workaround is to use array notation for the items with dashes. Rewriting the statement above would look like:

var myNum = result['data-sources']['data-source'].name;

If you have XML tags with attributes (as in "three" from the example above having attributes "boo" and "att"), you can get the values of these with a statement like the following:

var booVal = result.test.one.three['@boo'];

Will produce the answer "yah".Happy XML file parsing!Reference:

http://wiki.service-now.com/index.php?title=XMLDocument\_Script\_Object

View original source

https://www.servicenow.com/community/in-other-news/discovery-parsing-retrieved-xml-files/ba-p/2293717