- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2016 01:12 PM
Hi,
I am looking to do the following in ServiceNow... I have a client callable script include with the SOAP response (an XML doc). All I want to do is iterate through the XML document... and obtain all the elements that tag = 'value'.
Thanks in advance!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2016 05:50 AM
Hi Jesus,
I am not sure about the xml document i.e. what is the parent tag and what are the child tag, but the below script and example should help you out.
May be you can share the xml document which you need to parse.
Example xml document :
<soap:Envelope >
<soap:Body>
<BunchOfWordsResponse >
<BunchOfWordsResult>
<string>dog</string>
<string>cat</string>
<string>bird</string>
<string>fish</string>
<string>jupiter</string>
<string>putty</string>
<string>phone</string>
</BunchOfWordsResult>
</BunchOfWordsResponse>
</soap:Body>
</soap:Envelope>
Script to fetch value of all tag=string
var xmlString = the xml string , store it in this variable
var k=0;
var arr=[];
var xmldoc = new XMLDocument(xmlString);
var noParamNode = xmldoc.getNodes("//BunchOfWordsResponse/BunchOfWordsResult/*").getLength();
for(var k=1; k<noParamNode+1; k++){
var value = xmldoc.getNodeText("//string["+k+"]");
gs.print('Individual value for string tag is::'+value);
arr.push(value.toString());
}
gs.print('Array is::'+arr);
}
you can also refer to the below question for which I have given correct answer.
Using XPATH to get multiple values from the same node name
Mark my reply as Correct and also hit Like and Helpful if you find my response worthy.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2016 05:50 AM
Hi Jesus,
I am not sure about the xml document i.e. what is the parent tag and what are the child tag, but the below script and example should help you out.
May be you can share the xml document which you need to parse.
Example xml document :
<soap:Envelope >
<soap:Body>
<BunchOfWordsResponse >
<BunchOfWordsResult>
<string>dog</string>
<string>cat</string>
<string>bird</string>
<string>fish</string>
<string>jupiter</string>
<string>putty</string>
<string>phone</string>
</BunchOfWordsResult>
</BunchOfWordsResponse>
</soap:Body>
</soap:Envelope>
Script to fetch value of all tag=string
var xmlString = the xml string , store it in this variable
var k=0;
var arr=[];
var xmldoc = new XMLDocument(xmlString);
var noParamNode = xmldoc.getNodes("//BunchOfWordsResponse/BunchOfWordsResult/*").getLength();
for(var k=1; k<noParamNode+1; k++){
var value = xmldoc.getNodeText("//string["+k+"]");
gs.print('Individual value for string tag is::'+value);
arr.push(value.toString());
}
gs.print('Array is::'+arr);
}
you can also refer to the below question for which I have given correct answer.
Using XPATH to get multiple values from the same node name
Mark my reply as Correct and also hit Like and Helpful if you find my response worthy.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2020 07:53 PM
Hi,
I know that this is an older post, but how could I parse through an xml doc returned as below? This is a SOAP response. One of my confusion is the "a:" format in the element and how can I do additional parsing for the "Current Courses" elements that I need to insert in a list column. Any help would be great.
<?xml version="1.0" encoding="UTF-8"?> <s:Envelope > <s:Body> <SearchUserDetailR > <SearchUserDetailR > <a:Detail i:nil="true" /> <a:IsSuccessful>true</a:IsSuccessful> <a:Message i:nil="true" /> <a:ResultCode>Success</a:ResultCode> <a:Items> <a:UserDetailResponseItem> <a:Campus>Online</a:Campus> <a:Country /> <a:FirstName>Alan</a:FirstName> <a:FullName>Alan Just</a:FullName> <a:HomePhone>555-8566</a:HomePhone> <a:LastName>Just</a:LastName> <a:Manager /> <a:MobilePhone /> <a:PersonalEmailAddress i:nil="true" /> <a:EmailAddress>test@test.com</a:EmailAddress> <a:SourceSystemId>3</a:SourceSystemId> <a:StudentNumber i:nil="true" /> <a:StudentSchoolStatus i:nil="true" /> <a:SyCampId>6</a:SyCampId> <a:Title>Instructor</a:Title> <a:UserId>2</a:UserId> <a:UserName>Alan.Witty</a:UserName> <a:WorkPhone /> <a:WorkPhoneExtension>16</a:WorkPhoneExtension> <a:Advisor i:nil="true" /> <a:CurrentCourses> <a:UserDetailResponseCourseItem> <a:CourseCode>ECON2</a:CourseCode> <a:CourseLDA i:nil="true" /> <a:PrimaryInstructor>instruc1</a:PrimaryInstructor> <a:Section>03</a:Section> <a:Term>1234B</a:Term> </a:UserDetailResponseCourseItem> <a:UserDetailResponseCourseItem> <a:CourseCode>ECON2</a:CourseCode> <a:CourseLDA i:nil="true" /> <a:PrimaryInstructor>Instruc</a:PrimaryInstructor> <a:Section>01</a:Section> <a:Term>1234B</a:Term> </a:UserDetailResponseCourseItem> </a:CurrentCourses> <a:DegreeProgram i:nil="true" /> <a:MainAddressCity i:nil="true" /> <a:State>C</a:State> </a:UserDetailResponseItem> </a:Items> </SearchUserDetailR> </SearchUserDetailR> </s:Body> </s:Envelope>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2020 10:37 PM
Hi,
I would encourage to start a new thread for this as this is quite older and will be difficult to keep track of
please post the question link here once you raise that
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2020 05:36 AM
Thanks