This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

XML creation - java script

 
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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • 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
 
Bartender
Posts: 2325
100
Google Web Toolkit Eclipse IDE 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!