building an array with XML elements to populate a field

jesusemelendezm
Mega Guru

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!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

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);


}



xml-parsing.JPG


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


Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

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);


}



xml-parsing.JPG


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


Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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>

 

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks