1

Our app has a weird requirement which is to construct an xml using javascript and send that back to server as string.

I tried with jquery like this

$xmlT = $("<?xml version=\"1.0\" encoding=\"utf-8\"?><root></root>");

            $root = $("<notebook></notebook>"); //.attr("title", roottitle).            
            $root.attr("title", title);
            $root.attr("id", id);


            $root.appendTo($xmlT);

but am not able to get the xml as string back from the above variable.

is there some way or library using which i can construct xml and access that as string?

Regards, Jeez

Jeevan
  • 8,532
  • 14
  • 49
  • 67

3 Answers3

1

You can do things like createElement() etc on the XMLDoc returned from XMLHttpRequest.responseXML

http://www.w3schools.com/dom/dom_nodes_create.asp

So perhaps you can use XMLHttpRequest without actually doing a request, so you can get an object to manipulate.

You could then serialise the XML Dom using the XMLSerializer (for firefox) or xmlNode.xml for IE (metioned here How do I serialize a DOM to XML text, using JavaScript, in a cross browser way?)

Community
  • 1
  • 1
Alex KeySmith
  • 16,657
  • 11
  • 74
  • 152
0

You could take a look at this plugin

Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
0

I found these jquery plugins xml2json & json2xml which is close to what I was looking for, if not the same.

For more info check stackoverflow entryXML <-> JSON conversion in Javascript

Community
  • 1
  • 1
Jeevan
  • 8,532
  • 14
  • 49
  • 67