parsing a XML document

naresh1019
Mega Expert

can you please guide me on this.

1) I created a Soap OutBound Message.

2) recieved a long response(xml) for a given request.

3) i want to put this in a scheduled job.

4) I want to extract the a single node data from that and push it into cutom table.

5) I copied the preview script and pasted in scheduled script.

5) I want to parse the xml data and extrat the contents of the perticular node.

find_real_file.png

Please guide me on extracting the node data.

find_real_file.png

14 REPLIES 14

gcubed_
Tera Contributor

I usually do it like this:



var json = new JSON();


var obj = new XMLHelper().toObject(responseBody);


var response = json.encode(obj["soap:Body"]);


var retVal = json.decode(response.replace(new RegExp("wsa:", 'g', "")); //remove namespace for dot object if needed


var projects = retVal.GetPortfoliosResult.PortfolioDescriptor.Projects;



V/r,


Gene


naresh1019
Mega Expert

It isshowing some error.......


can you show me the exact code if possible.


Like this:



var responseBody = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Header/><soapenv:Body><SessionID xmlns="http://www.niku.com/xog">3299-07373345B5-91D3-2AB6474F7335</SessionID><QueryResult xmlns="http://www.niku.com/xog/Query"><Code>strips</Code><Records><Record><concat_id>520923851070845024001</concat_id><strip_id>5209238</strip_id><obligated>804.06</obligated><expended>1804.06</expended><project>Medical Theater</project><year_execution>2014</year_execution></Record><Record><concat_id>520923951070845024001</concat_id><strip_id>5209239</strip_id><obligated>245</obligated><expended>2002.56</expended><project>Awareness Theater</project><year_execution>2014</year_execution></Record><Record><concat_id>520924651071255074027</concat_id><strip_id>5209246</strip_id><obligated>632.22</obligated><expended>632.22</expended><project>JTLTE Theater</project><year_execution>2016</year_execution>></Record></Records><Slice><Number>0</Number><Size>716</Size><Total>716</Total></Slice></QueryResult></soapenv:Body></soapenv:Envelope>';



var json = new JSON();



var obj = new XMLHelper().toObject(responseBody);



var response = json.encode(obj["soapenv:Body"]);



var retVal = json.decode(response);



var results = retVal.QueryResult.Records;



//JSUtil.logObject(retVal.QueryResult.Records, " -> ");



for (var i = 0; i < results.Record.length; i++) {


      gs.log(results.Record[i].project + ' - ' + results.Record[i].expended);


}



find_real_file.png



V/r,


Gene


naresh1019
Mega Expert

Hi Gene,



Thanks for the response....


I have the following xml(coverted to object typr using XML helper function)




find_real_file.png



i want to extract 13 names


Ashish Sahu1
Tera Contributor

we have below xml request:

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/><SOAP-ENV:Body>
<tns:CreateIncidentVBMRequest xmlns:tns="http://group.vodafone.com/schema/vbm/service/incident/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tns:IncidentVBO>
<cmn:IDs xmlns:cmn="http://group.vodafone.com/schema/common/v1">
<cmn:ID>NET09552959</cmn:ID>
</cmn:IDs>
<cmn:Name xmlns:cmn="http://group.vodafone.com/schema/common/v1">TEST TICKET</cmn:Name>
<cmn:Desc xmlns:cmn="http://group.vodafone.com/schema/common/v1">
NO MATCH FOUND.</cmn:Desc>
<cmn:Type xmlns:cmn="http://group.vodafone.com/schema/common/v1">Proactive Incident</cmn:Type>
<cmn:Status xmlns:cmn="http://group.vodafone.com/schema/common/v1">New</cmn:Status>
<cmn:Created xmlns:cmn="http://group.vodafone.com/schema/common/v1">14/01/2021 14:28:09</cmn:Created>
<vbo:Details xmlns:vbo="http://group.vodafone.com/schema/vbo/service/incident/v1">
<vbo:SeverityCode>3</vbo:SeverityCode>
<vbo:PriorityCode>3</vbo:PriorityCode>
</vbo:Details>
<vbo:Roles xmlns:vbo="http://group.vodafone.com/schema/vbo/service/incident/v1">
<vbo:Requester>
<cmn:Organisation xmlns:cmn="http://group.vodafone.com/schema/common/v1">
<cmn:IDs>
<cmn:ID schemeName="ProfileID">RANK1232145</cmn:ID>
<cmn:ID schemeName="VodafoneProfileID"/>
</cmn:IDs>
<cmn:Name>RANK</cmn:Name>
<cmn:LocalTradingName>MorrisonsB2B</cmn:LocalTradingName>
</cmn:Organisation>
<vbo:ContactPoints>
<vbo:ContactPoint>
<cmn:Email xmlns:cmn="http://group.vodafone.com/schema/common/v1">
<cmn:FullAddress/>
</cmn:Email>
<cmn:Telephone xmlns:cmn="http://group.vodafone.com/schema/common/v1">
<cmn:SubscriberNumber/>
</cmn:Telephone>
</vbo:ContactPoint>
</vbo:ContactPoints>
</vbo:Requester>
</vbo:Roles>
<vbo:Parts xmlns:vbo="http://group.vodafone.com/schema/vbo/service/incident/v1">
<vbo:Services>
<vbo:Service>
<cmn:Name xmlns:cmn="http://group.vodafone.com/schema/common/v1">VONE-C SERVICE</cmn:Name>
<vbo:ServiceArea>VOICE VONE-C</vbo:ServiceArea>
<vbo:Specification>
<cmn:IDs xmlns:cmn="http://group.vodafone.com/schema/common/v1">
<cmn:ID>0</cmn:ID>
</cmn:IDs>
</vbo:Specification>
</vbo:Service>
</vbo:Services>
<vbo:IncidentSites>
<vbo:IncidentSite>
<cmn:IDs xmlns:cmn="http://group.vodafone.com/schema/common/v1">
<cmn:ID>NO SITE</cmn:ID>
</cmn:IDs>
<cmn:Name xmlns:cmn="http://group.vodafone.com/schema/common/v1">NO SITE</cmn:Name>
<vbo:ContactPoints>
<vbo:ContactPoint>
<cmn:Postal xmlns:cmn="http://group.vodafone.com/schema/common/v1">
<cmn:Street/>
<cmn:Town/>
<cmn:Country/>
<cmn:County/>
<cmn:PostCode/>
</cmn:Postal>
</vbo:ContactPoint>
</vbo:ContactPoints>
</vbo:IncidentSite>
</vbo:IncidentSites>
<vbo:Symptom>
<cmn:Name xmlns:cmn="http://group.vodafone.com/schema/common/v1">CALL TRANSFER</cmn:Name>
<cmn:Categories xmlns:cmn="http://group.vodafone.com/schema/common/v1">
<cmn:Category>EXTENSION MOBILITY CROSS CLUSTER</cmn:Category>
</cmn:Categories>
<vbo:DiscoveredDateTime>14/01/2021 14:28:09</vbo:DiscoveredDateTime>
</vbo:Symptom>
</vbo:Parts>
</tns:IncidentVBO>
</tns:CreateIncidentVBMRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

 

In this we are trying to fetch nodes value but getting null everytime, Can you please help in that, below we are using to fetch nodes and its value,

 var xmlDoc = new XMLDocument(requestXML,true);

var vshort_description = this.xmlDoc.getNodeText("//Body/CreateIncidentVBMRequest/IncidentVBO/Name");
var vstatus = this.xmlDoc.getNodeText("//Body/CreateIncidentVBMRequest/IncidentVBO/Status");

 

it tried it in through backgroun script but did not work,

var nodeName = gs.getMessage('Test XML');
//gs.print(nodeName);
var xmlDoc = new XMLDocument(nodeName,true);
var vshort_description = xmlDoc.getNodeText("//SOAP-ENV:Body/CreateIncidentVBMRequest/IncidentVBO/Name");

gs.print(vshort_description);

 

'Test XML :- contains same request xml