views:

264

answers:

1

I need to consume XML based RESTful API. All request are sent in XML format. I've been using MooTools extensions to build XML requests. However, I don't know how to transform it string, so that I could send the XML directly to server.

var reqEl = new Element('req');
var loginEl = new Element('login');
var usernameEl = new Element('username',{text: login});
var hashEl = new Element('hash', {text: pass});
loginEl.inject(usernameEl);
loginEl.inject(hashEl);
reqEl.inject(loginEl);

This code generates following XML:

<req>
 <login>
   <username>peter123</username>
   <hash>123abc</hash>
 </login>
</req>

Is there any way the element object to string? Or should I always build XML requests manually? Something like this:

var q = "<req><login><username>" + escape(login) + "</username><hash>" + pass + "</hash></login></req>";

Thanks in advance!

A: 

I made some tries, and I guess that building the request manually is the right way.

Element() constructor is focused on (x)HTML elements, so the browser will not collaborate.

As far as I noticed, even if injecting the hashEl on the body returns right data, it will not usable for your purpose.

Some times ago I had to solve a task like this one, and working with string was the solution, for me, and did not cause any problem or restriction.

Enrico Carlesso