XML construction using Javascript - javascript

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

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?)

You could take a look at this plugin

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

Related

XML parsing using Jquery where format is unknown

I dont know the XML format. but i want to parse it from a given XMLObject using Jquery. Is it possible if yes then how to do that.
You can do this using JSONP to convert your XML to a JSON object.
This blog posts covers it nicely and saves me rewriting the code.
http://weedygarden.net/2011/01/consuming-remote-xml-as-jsonp/

Assign json stored in a file to a js variable

I am working on a project and I was not able to figure out how to do this. I have a json file and need to assign it to a js variable to use the protovis visualization tools.
I tried searching on google but could not find any. Please let me know if someone knew how to do this. Thanks!
Use jQuery's parseJSON().
Description: Takes a well-formed JSON string and returns the resulting
JavaScript object. version added: 1.4.1jQuery.parseJSON( json )
jsonThe JSON string to parse. Passing in a malformed JSON string may
result in an exception being thrown.
If you're getting the file from your server via the XMLHttpRequest object, you can parse the json using the JSON.parse function:
var myJson = JSON.parse(response);
For browsers that don't support the JSON object you can get a library for it here: http://www.json.org/js.html

json: how to get the json-string into a javascript object

i am converting my ajax code from xml to json, but i am missing something basic here:
when i receive the json-string on the client-side, what is the recommended way to convert it into a javascript object.
for example i get this string:
{"connectionid":12345}
and i would like to do something like this:
alert(xmlhttp.responseText.connectionid);
thanks!
Use JSON.parse(), or eval(), if you like to live dangerously (or fully trust where your JSON comes from).
If you happen to be using jQuery, you get $.parseJSON().
Most browsers (the recent ones at least.. not IE7) have a native JSON object that you can use to parse and stringify JSON.
alert(JSON.parse(xmlhttp.responseText).connectionid);
In browsers that don't support the JSON object, you can either use a JSON parser from JSON.org or use eval(), however eval() is quite dangerous and i definitly don't advise you to use it.
Call eval on the response text.
var response = eval(xmlHttp.responseText);
alert(response.connectionId);
you could use eval
check this out : http://www.json.org/js.html
edit - oops, others typed faster :(

Cross-Browser Javascript Parser for XML with Namespace

This subject has been touched on before, but there's been some time since the last question regarding namespace handling.
Is there a cross-browser solution to get the elements by name in Javascript?
<?xml version="1.0" encoding="UTF-8"?>
<NS:response success="1" xmlns:NS="http://someURI/ns">
<NS:user firstname="foo" lastname="bar"></NS:user>
<NS:cookie value="2c0ea35bcac2e05d439609367a236b28" name="session"></NS:cookie>
</NS:response>
So far what I've got:
var oXML = (new DOMParser()).parseFromString(xmlstring, "text/xml");
var root = oXML.documentElement;
var user = typeof(user=root.getElementsByTagName(root.prefix + ':user')[0]) === "undefined"
?root.getElementsByTagName('user')[0]
:user;
Hasn't been tested in IE, but if anyone has any cross-browser solution, I'd be willing to hear.
Other Considerations:
getElementsByTagNameNS() - am trying to avoid having to specify the namespace/uri
using regex to strip the namespace before creating the XML document
not using a namespace - I have that option, but would not like to go that route
You could try another approach, by converting the XML to JSON server side, using a generic XSLT like http://code.google.com/p/xml2json-xslt/, and deliver to the browser only JSON.
It will add up a small overhead on the server response, but nothing compared to the amount of code and time spent on the browser to render XML.
With the exception of IE, with its impressive msxml, I think reading XML in common browsers is a real pain compared to JSON.
Using a JS framework like jQuery or Prototype for this kind of ajax scripts would help.
You can also do (example) $("user[name=foo]") that will select all your user tags with with name=foo.
It's a solution that many users managed to do to handle elements selection by name. And $("tag[name=foo]") is crossbrowser.

Storing HTML in datastore and Decoding during XSL transformation

I'm trying to store html object tags for video players in a datastore. This data will be serialized and passed back to client javascript where it will be transformed and displayed in the browser to show the video. I need to be able to htmlDecode the data so that it is evaluated properly in the browser.
Any ideas how to accomplish this in javascript?
Grab your HTML code and run through one of the methods described in this article (I had good results with encodeURI). When ready to use - run it through the compatible decode method
One of my co-workers suggested the following solution:
http://www.strictly-software.com/htmlencode
This is a set of javascript libraries to encode and decode html.
Why not use the following method?
HttpUtility.HtmlEncode(...)
And then on the JavaScript side, use the following.
unescape(theEncodedHtml)

Categories

Resources