Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XML creation - java script

 
Neo Wills
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was looking at ajax with javascript and I see that there is feature for parsing a XML file and populating content. But on the reverse, when some data needs to be sent, can it be sent in XML (is there a generator for it)? If so, is it better to create the XML file at the server end using some servlet / jsp / asp techniques or is it better to do it at the client end in ajax+js (From what I looked around, I dont see folks creating xml file in their javascript code).

Regards,
Bharath
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most server side technologies don't automatically know how to parse XML so if you send XML to the server you are left with the task of parsing it. Yuck. I'd just stick with simple query params when posting data back to the server. And for the record, I avoid sending XML to the client if at all possible. JavaScript is much more friendly with JSON and if you must send XML, using a library like jQuery makes parsing it much easier.
 
Neo Wills
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Gregg. I took a look at JSON and looks interesting.

If you feel passing the data over http on xml (kind of) structure, is there any reason behind it? Isnt the DOM / SAX parsers on java good enough to parse these?

Also on the reverse side, the server passing XML-RPC data (say JSON itelf), wouldnt it be a cleaner approach?

Bharath
 
salvin francis
Bartender
Posts: 1263
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not against JSON,

But i dont understand whats wrong in parsing xml in javascript ?
I dont think you even have to parse it, the browser does the parsing.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:I am not against JSON,

But i dont understand whats wrong in parsing xml in javascript ?
I dont think you even have to parse it, the browser does the parsing.


That's not entirely true. You still have to traverse it. Let's assume an XML structure like this....



To get the contents of the first employee tag you would need to do something like



That might not seem too bad but this is a simple bit of XML. Imagine something more complex and needing to loop over children of children of children to get elements you need. If you consider the same code as JSON, to get the first employee you would do something like...



Because when JSON is eval'd in JavaScript it becomes a real object with actual properties. Not nodes and attributes. A library like jQuery can alleviate some heartaches of parsing XML but JSON is by far the simplest regardless.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bharath, yes, there are some excellent ways to parse XML on the server using Java. But consider this. You have an object to populate with the data you get back from the server. This data is going to be in a Map (HttpServletRequest). It's already structured for you. Simply pull the values out of the map. Why the extra step of parsing XML?

With regards to XML-RPC, like I said, I avoid sending XML if at all possible. Sometime it does make more sense (SOAP, WSDL, etc). You asked a pretty generic question with no real context and that is what I was giving an answer to. If you would like to get into more specifics about your problem domain then I'm sure we could provide some pinpointed advice to that situation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic