Building nested XML

Sue Frost
Giga Guru

I'm trying to build some XML that will be sent via MQ to a print facility. This is in a scoped application. I've been staring at this for several days and haven't gotten closer to a solution. I've tried "appendChild" and "setCurrent" without success. Eventually, I'll need to nest into locations further with as each location can have multiple coverages, etc. But first steps first. What am I missing here?

TIA!

This is the XML that I need to get to:

1 <soapenv:Envelope xmlns="http://www.XXXXXX.com/ZZZZZZ1.0.0/xml/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

2           <soapenv:Header/>

3           <soapenv:Body>

4           <quote>

5                     <version>

6                               <userId>SUF</userId>

7                     </version>

8                     <contract>

9                               <sysID>4917b8e24f023a805a1e4bff9310c775</sysID>

10                               <printLanguage>fq</printLanguage>

11                     </contract>

12                     <locations>

13                               <testElement>test element</testElement>

14                               <location>

15                                         <locationAddress1>Address Line 1</locationAddress1>

16                                         <locationAddress2>Address Line 2</locationAddress2>

17                                         <locationAddressCity>test</locationAddressCity>

18                                         <locationAddressProv>Ontario</locationAddressProv>

19                                         <locationAddressPC>N2H4L4</locationAddressPC>

20                               </location>

21                               <location>

22                                         <locationAddress1>Address Line 1</locationAddress1>

23                                         <locationAddress2>Address Line 2</locationAddress2>

24                                         <locationAddressCity>New Hamburg</locationAddressCity>

25                                         <locationAddressProv>Ontario</locationAddressProv>

26                                         <locationAddressPC>N0G 2B</locationAddressPC>

27                             </location>

28                     </locations>

29           </quote>

30 </soapenv:Body>

31</soapenv:Envelope>

And this is as far as I can get:

1 <soapenv:Envelope xmlns="http://www.XXXXXX.com/ZZZZZZ1.0.0/xml/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

2           <soapenv:Header/>

3           <soapenv:Body>

4                     <quote>

5                               <version>

6                                         <userId>SUF</userId>

7                               </version>

8                               <contract>

9                                         <sysID>4917b8e24f023a805a1e4bff9310c775</sysID>

10                                         <printLanguage>fq</printLanguage>

11                               </contract>

12                               <locations>

13                                         <testElement>test element</testElement>

14                                       <location>

15                                                   <locationAddress1>Address Line 1</locationAddress1>

16                                                   <locationAddress2>Address Line 2</locationAddress2>

17                                                   <locationAddressCity>test</locationAddressCity>

18                                                   <locationAddressProv>Ontario</locationAddressProv>

19                                                   <locationAddressPC>N2H4L4</locationAddressPC>

20                                                   <locationAddress1>Address Line 1</locationAddress1>

21                                                   <locationAddress2>Address Line 2</locationAddress2>

22                                                   <locationAddressCity>New Hamburg</locationAddressCity>

23                                                   <locationAddressProv>Ontario</locationAddressProv>

24                                                   <locationAddressPC>N0G 2B</locationAddressPC>

25                                       </location>

26                                       <location/>

27                               </locations>

28                     </quote>

29           </soapenv:Body>

30</soapenv:Envelope>

Here's the script in my UI Action:

requestPDF();

function requestPDF(){

  var exUtil = new global.TranslationScripts();

  var requestBody;

  var responseBody;

  var status;

  var soapMessage;

  gs.addInfoMessage('SUF prep PRINT');

  try{

  gs.info("SUF - into TRY loop");

  soapMessage = new sn_ws.SOAPMessageV2("Extremev4", "getExtremeQuote");   // Might throw exception if message doesn't exist/not visible due to scope

  soapMessage.setBasicAuth("admin","admin");

  soapMessage.setEndpoint("https://wstest.teig.com/dt2/ExtremeRatingPOC2/request");

  var xmlDoc = new XMLDocument2();

  xmlDoc.parseXML('<quote></quote>');

  var rootNode = xmlDoc.getDocumentElement();

  //VERSION INFO

  xmlDoc.setCurrentElement(rootNode);

  xmlDoc.createElement('version');

  var versionNode = xmlDoc.getNode('quote/version');

  xmlDoc.setCurrentElement(versionNode);

  xmlDoc.createElementWithTextValue('userId', 'SUF');

  //CONTRACT INFO

  xmlDoc.setCurrentElement(rootNode);

  xmlDoc.createElement('contract');

  var contractNode = xmlDoc.getNode('quote/contract');

  xmlDoc.setCurrentElement(contractNode);

  //Write out contract details

  xmlDoc.createElementWithTextValue('sysID', current.sys_id);

  xmlDoc.createElementWithTextValue('printLanguage', current.print_language);

  //LOCATION INFO

  xmlDoc.setCurrentElement(rootNode);

  xmlDoc.createElement('locations');

  var locationsNode = xmlDoc.getNode('quote/locations');

  xmlDoc.setCurrentElement(locationsNode);

  xmlDoc.createElementWithTextValue('testElement', "test element");

  var locationGR = new GlideRecord('x_enig_quote_extrm_location');

  locationGR.addQuery('parent', current.sys_id);

  locationGR.query();

  while(locationGR.next()) {

  //xmlDoc.setCurrentElement(rootNode);

  xmlDoc.createElement('location');

  var locNode = xmlDoc.getNode('quote/locations/location');

  xmlDoc.setCurrentElement(locNode);

  //write out address

  xmlDoc.createElementWithTextValue('locationAddress1', 'Address Line 1');

  xmlDoc.createElementWithTextValue('locationAddress2', 'Address Line 2');

  xmlDoc.createElementWithTextValue('locationAddressCity', locationGR.location_city);

  xmlDoc.createElementWithTextValue('locationAddressProv', locationGR.location_province.getDisplayValue());

  xmlDoc.createElementWithTextValue('locationAddressPC', locationGR.location_postal_code);

  //step up one level?

  xmlDoc.setCurrentElement(locationsNode);

  }

  gs.info('Curr XML ' + xmlDoc);

  //Add XML Envelope

  var workDoc = xmlDoc.toString();

  var stripDoc = workDoc.substring(38, workDoc.length);

  gs.info('Curr Strip ' + stripDoc);

  var envDoc = '<soapenv:Envelope xmlns="http://www.economical.com/ExtremeRating1.0.0/xml/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header/><soapenv:Body>' + stripDoc + '</soapenv:Body></soapenv:Envelope>';

  var xmlEnvelopeDoc = new XMLDocument2();

  xmlEnvelopeDoc.parseXML(envDoc);

  soapMessage.setRequestBody(envDoc);

  gs.info('soapMessageHead is ' + soapMessage.getRequestHeaders());

  gs.info('soapMessageBody is ' + soapMessage.getRequestBody());

  soapMessage.setHttpTimeout(50000); //In Milli seconds. Wait at most 10 seconds for response from http request.

  response = soapMessage.execute();//Might throw exception if http connection timed out or some issue with sending request itself because of encryption/decryption of password and stuff

  responseBody = response.haveError() ? response.getErrorMessage() : response.getBody();

  status = response.getStatusCode();

  gs.addInfoMessage("SUF Print COMPLETE");

  }

  catch(e)

  {

  gs.info("Trouble: " + e);

  //responseBody = e.getMessage();

  status = '500';

  }

  finally

  {

  requestBody = soapMessage ? soapMessage.getRequestBody():null;

  }

  gs.info("Request Body: " + requestBody);

  gs.info("Response: " + responseBody);

  gs.info("HTTP Status: " + status);

  action.setRedirectURL(current);

}

1 ACCEPTED SOLUTION

drbz
Mega Expert

Hi,


you can use appendChild. Please try with following while block, this should work:



while(locationGR.next()) {



  //xmlDoc.setCurrentElement(rootNode);


  var loc = xmlDoc.createElement('location');


  locationsNode.appendChild(loc);


  //var locNode = xmlDoc.getNode('quote/locations/location');


  xmlDoc.setCurrentElement(loc);



  //write out address


  xmlDoc.createElementWithTextValue('locationAddress1', 'Address Line 1');


  xmlDoc.createElementWithTextValue('locationAddress2', 'Address Line 2');


  xmlDoc.createElementWithTextValue('locationAddressCity', locationGR.location_city);


  xmlDoc.createElementWithTextValue('locationAddressProv', locationGR.location_province.getDisplayValue());


  xmlDoc.createElementWithTextValue('locationAddressPC', locationGR.location_postal_code);



  //step up one level?


  //xmlDoc.setCurrentElement(locationsNode);



  }


View solution in original post

3 REPLIES 3

drbz
Mega Expert

Hi,


you can use appendChild. Please try with following while block, this should work:



while(locationGR.next()) {



  //xmlDoc.setCurrentElement(rootNode);


  var loc = xmlDoc.createElement('location');


  locationsNode.appendChild(loc);


  //var locNode = xmlDoc.getNode('quote/locations/location');


  xmlDoc.setCurrentElement(loc);



  //write out address


  xmlDoc.createElementWithTextValue('locationAddress1', 'Address Line 1');


  xmlDoc.createElementWithTextValue('locationAddress2', 'Address Line 2');


  xmlDoc.createElementWithTextValue('locationAddressCity', locationGR.location_city);


  xmlDoc.createElementWithTextValue('locationAddressProv', locationGR.location_province.getDisplayValue());


  xmlDoc.createElementWithTextValue('locationAddressPC', locationGR.location_postal_code);



  //step up one level?


  //xmlDoc.setCurrentElement(locationsNode);



  }


That worked! Thank you!


I had to make one small change - the last line (stepping up one level) is needed. Otherwise, I keep nesting deeper and deeper. So my final solution is:



while(locationGR.next()) {



  //xmlDoc.setCurrentElement(rootNode);


  var loc = xmlDoc.createElement('location');


  locationsNode.appendChild(loc);


  //var locNode = xmlDoc.getNode('quote/locations/location');


  xmlDoc.setCurrentElement(loc);



  //write out address


  xmlDoc.createElementWithTextValue('locationAddress1', 'Address Line 1');


  xmlDoc.createElementWithTextValue('locationAddress2', 'Address Line 2');


  xmlDoc.createElementWithTextValue('locationAddressCity', locationGR.location_city);


  xmlDoc.createElementWithTextValue('locationAddressProv', locationGR.location_province.getDisplayValue());


  xmlDoc.createElementWithTextValue('locationAddressPC', locationGR.location_postal_code);



  //step up one level?


  xmlDoc.setCurrentElement(locationsNode);