XML Data Source - X-path assistance

scwillson
Mega Guru

I am trying to import XML data, but cannot get the my desired result, and was hoping for some assistance. Part of the problem is that the XML nodes are mostly <col> each with different names. Ideally, I would like to make ServiceNow pull the <col>'s name attribute as the Table column name, and then pull the value of that node. Here is an example of what I am working with:

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

<knowledge_documents count="1747">

        <row id="400430">

                  <col name="DOC_UUID" type="uuid">427B1963B812B4DA</col>

                  <col name="CREATION_DATE" type="date">1367352371</col>

                  <col name="RESOLUTION" type="string">&lt;b&gt;This&lt;b&gt; is my test data&lt;ol&gt;&lt;li&gt;1&lt;/li&gt;&lt;li&gt;2&lt;/li&gt;&lt;/ol&gt;</col>

                  <categories>

                            <category>TOP&gt;Internal Knowledge&gt;Office&gt;Software&gt;Addin FS</category>

                  </categories>

        </row>

        <row id="498423">

                  <col name="DOC_UUID" type="uuid">428F3443B1784BA</col>

                  <col name="CREATION_DATE" type="date">1367352371</col>

                  <col name="RESOLUTION" type="string">&lt;b&gt;This&lt;b&gt; is my test data&lt;ol&gt;&lt;li&gt;1&lt;/li&gt;&lt;li&gt;2&lt;/li&gt;&lt;/ol&gt;</col>

                  <categories>

                            <category>TOP&gt;Internal Knowledge&gt;Office&gt;Software&gt;Addin FS</category>

                  </categories>

        </row>

</knowledge_documents>

No matter what X-Path I specify, Servicenow will not see the <col> nodes.

Here are some x-paths I've tried:

  • /knowledge_documents/row (original x-path)
  • /knowledge_documents/row/*
  • //col[@name='TITLE']

Still can't get it to work. Is it possible to pull only specific data according to attribute data? I know X-Path can, but I couldn't get it working in ServiceNow.

3 REPLIES 3

dmfranko
Kilo Guru

Something like this should work for you:



var xmldoc = new XMLDocument(xmlString);


// Whatever attribute you want to get


nodelist = xmldoc.getNodes("//row/col[@name='DOC_UUID']");



for(i=0;i<nodelist.getLength();i++){


      gs.print(nodelist.item(i).getTextContent());


}



You don't have to specify only columns in a row, but it can't hurt.   So you could have the below for your xpath as well.



nodelist = xmldoc.getNodes("//col[@name='DOC_UUID']");


Where are you entering that script?



When I'm building the Data Source, from the Data Source module, I only have a single string field to enter the X-Path in.


find_real_file.png


I misunderstood where you were doing this.   I was just running a background script, but you're right that it's not working quite as expected when doing it as a data source.   I'll see if I can figure it out.