Discovery pattern to parse XML with attributes?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-09-2023 04:58 PM
I'm trying to create a Discovery pattern to parse an SN MID server's config.xml file but I'm having issues getting it to work.
The "Parse XML file" pattern action works well for the below kind of XML, where you just want the inner text. I can use the mouse tool to click and highlight an element name and BAM it works.
<data>
<serial>MyCoolSerialNumber</serial>
</data>
But it seems to struggle with key-value attribute XML elements, like the ones used in the ServiceNow MID server config.xml file.
<parameters>
<parameter name="url" value="https://dev.service-now.com"/>
<parameter name="name" value="MyMid02"/>
</parameters>
I've tried a few different XML XPath strings, but when I click on "Test" in the pattern designer I either get:
"Test failed: Make sure the steps are compatible with the current attributes and variables values."
What's a way to get a pattern to read a parameter value from such an XML structure?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-09-2023 05:46 PM - edited ‎10-09-2023 06:30 PM
I did find a way to do it, but it feels like the long way around, but it works.
- Use a "Parse File" action to read all of the parameters in the XML structure into a table variable.
$config_table
config_name = /parameters/parameter/@name
config_value = /parameters/parameter/@value - Use a "Filter table" action to get the row for the attribute I want, and put that in a table with 1 row in it.
WHERE $mid_config_table[].config_name EQUALS "name" - Use a "Set Parameter Value" action to set the value based on the filtered table.
NAME $mid_name VALUE $mid_config_name[1].config_value
It also relies on the fact that all of the <parameter> elements in the XML structure have both a "name" and a "value". Any lop-sided parameter elements will mess up the $config_table and shuffle values against different names. However, this is unlikely in the MID server's configuration file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-22-2023 08:18 PM
@David McDonald Thanks for posting the solution. You are a life saver!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2024 01:08 AM
Hey @David McDonald ,
I have requirement to read json file from server but I am very much new to this, Could you please provide some guidance how to create pattern and test it?
Thanks in advance