3

I need to send a lot of information from my website to my webserver. These information contain data (first name, last name, etc.) from several users.

Before I'll send these information with an AJAX request to the webserver, I have to extract them out of a table and organize them somehow.

Now I'm considering two different possibilities:

1: Organize the data in an XML document with JavaScript / jQuery and send it to the webserver or ...

XML document

<create>
    <customer>
        <first-name>foo</first-name>
        <last-name>bar</last-name>
        ...
    <customer>
</create>

2: store the data in a string an organize them later on the webserver with PHP.

string:

   "first-name='foo';last-name='bar'"

I've already tried this to create a XML document ...

    var xmlDocument = $.parseXML('<create/>');
    var customer = xmlDocument.createElement('customer');
    xmlDocument.documentElement.appendChild(customer);
    var firstName = xmlDocument.createElement('first-name');
    xmlDocument.documentElement.appendChild(firstName);

... but it wasn't really working out. I used xmlDocument.find('first-name') to check if it's built correctly but it failed. Perhaps I'm accessing the XML document wrong.

I've also tries to use jQuery.parseXML(); but it wasn't working either.

Well, what is the proper way to create / access a XML document, create / delete / edit nodes and textnodes and set / get attributes in JavaScript / jQuery.

Can you give me an advice which of my mentioned opportunity to use? Maybe there is even one I haven't considered yet. I'd appreciate it if you could add some sample code.

2 Answers 2

2

You should do something like

var xmlDocument = $('<create/>');
var customer = $('<customer/>');
xmlDocument.append(customer);
var firstName = $('<first-name/>').text('john');
xmlDocument.append(firstName);

Corresponding xml:

<create>
  <customer></customer>
  <first-name>john</first-name>
</create>

To find an element xmlDocument.find('first-name').text() fiddle here http://jsfiddle.net/YpfmD/

This is exactly how you'd create html elements (after all html is an XML)

if yu need to create element with attributes you can do

 $('<name/>', { first: "john", second: "doe"});
 //this creates <name first="jhon" second="doe"></name>
Sign up to request clarification or add additional context in comments.

2 Comments

how do I access a node and get the value?
@sebbl.sche justdo xmlDocument.find('first-name').text() (i updated my answer)
0

Why can't you create the string (xml or whatever) using server-side scripting, like PHP? It's much easier that way.

But if you need to use jquery/javascript, I'd suggest you go the JSON approach.

Have a look here:

jquery .serializeArray()

jquery .parseJSON

You could try this?

2 Comments

Is it also possible to store multiple users in a JSON object? Something like: var obj = $.parseJSON('{"user1" : {"first-name" : "foo", "last-name" : "bar"}, "user2" : ... }');
JSON is just string representation of almost any type of object you can think of. It's a lot similer to XML in the regard that it stores data in a well-defined structure, so that one can easily extract info from that again. It is also very popular in ajax circles, because most of the time it takes up less space than XML. Go read up on it a bit and play around with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.