Topic
4 replies Latest Post - ‏2012-10-29T22:36:44Z by SystemAdmin
SystemAdmin
SystemAdmin
2327 Posts
ACCEPTED ANSWER

Pinned topic Recommended approach for creating XML request

‏2012-10-22T23:01:15Z |
Is there a best practice way in WL to prepare an XML message request to send to a backend system via WL adapter? Are there any built in libraries to do this using JS, or should Java be used? Right now we have built a few using JS string concatenation but this seems so 12 years ago doing it this way as the messages is more than a few lines.

Thanks.
  • SystemAdmin
    SystemAdmin
    2327 Posts
    ACCEPTED ANSWER

    Re: Recommended approach for creating XML request

    ‏2012-10-23T09:27:31Z  in response to SystemAdmin
    You can do the following from client side JavaScript:

    
    <script src=
    'http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js'></script>   var source = 
    "<xml><hello>{{msg}}</hello></xml>"; 
    //variables you want to replace are inside {{ }} var template = Handlebars.compile(source); var context = 
    {msg: 
    'world'
    }; var xml = template(context); console.log(xml); 
    //"<xml><hello>world</hello></xml>" 
    //send xml to the adapter or whatever you want to do with it
    


    More/Examples/Source: http://handlebarsjs.com/

    Handlebars is a superset of Mustache (another great alternative):
    http://mustache.github.com/
    • SystemAdmin
      SystemAdmin
      2327 Posts
      ACCEPTED ANSWER

      Re: Recommended approach for creating XML request

      ‏2012-10-25T19:54:04Z  in response to SystemAdmin
      Thank you very much. This is much better. I wouldn't have thought a third-party library was needed as I would have expected a product with an adapter concept to include it natively. However, on the other hand it is probably better for flexibility as it allows the developer and project to use a preferred library than one being imposed.
      • SystemAdmin
        SystemAdmin
        2327 Posts
        ACCEPTED ANSWER

        Re: Recommended approach for creating XML request

        ‏2012-10-29T20:17:22Z  in response to SystemAdmin
        Is there a way to do this using server side javascript within the adapter code? It becomes messy trying to pass XML fragments via client side to adapter code as the logic to build repeating sections etc is within the adapter. How can I get the adapter framework to recognize and utilize the handlebars framework?
        • SystemAdmin
          SystemAdmin
          2327 Posts
          ACCEPTED ANSWER

          Re: Recommended approach for creating XML request

          ‏2012-10-29T22:36:44Z  in response to SystemAdmin
          "Is there a way to do this using server side javascript within the adapter code? "

          Try pasting in the code for mustache or handlebars:
          https://raw.github.com/janl/mustache.js/master/mustache.js
          http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js

          Not sure if it works, let me know if you try it. Adapter code runs using Rhino: https://developer.mozilla.org/en-US/docs/Rhino

          Alternatively, just replace keys yourself:

          
          var source = 
          "<xml><hello>{{msg}}</hello></xml>"; var result = source.replace(
          "{{msg}}", 
          'world'); console.log( result ); 
          //"<xml><hello>world</hello></xml>"