Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to retrieve attributes from an XML

andreazaffaroni
Tera Expert

<?xml version="1.0" encoding="UTF-8" ?>
<test>
        <one>
                <two prova="ANNA">A</two>
               <three>B</three>
       </one>
       <one>
              <two prova="OSSO">C</two>
             <three>D</three>
      </one>
</test>

 

Hi community,

I need to retrieve the attributes from both nodes two: "ANNA" and "OSSO", how can I do this?

I am using XMLDocument2

Thanks you

Andrea

1 ACCEPTED SOLUTION

No direct method like XMLDocument to fetch attributes. You can use this one which retrieves by index

var xmlString = "<test><one><two prova='ANNA'>A</two><three>B</three></one><one><two prova='OSSO'>C</two><three>D</three></one></test>";
var xmlDoc = new XMLDocument2(xmlString);
xmlDoc.parseXML( xmlString ); 

var node = xmlDoc.getNode("//test");
var childNodes = node.getChildNodeIterator();
while(childNodes.hasNext()){
     var i = childNodes.next().toString();
     var index = i.indexOf("prova");
     if(index  > -1){
       var end = i.indexOf("\">", index);
       gs.print(i.substring(index+7, end));
     }
}

View solution in original post

5 REPLIES 5

Thank you,

Andrea