File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes XML creation - java script Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "XML creation - java script" Watch "XML creation - java script" New topic
Author

XML creation - java script

Neo Wills
Ranch Hand

Joined: Nov 29, 2006
Posts: 40
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

Joined: Jul 11, 2001
Posts: 15300
    
    6

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.


GenRocket - Experts at Building Test Data
Neo Wills
Ranch Hand

Joined: Nov 29, 2006
Posts: 40
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
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

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.


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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

Joined: Jul 11, 2001
Posts: 15300
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XML creation - java script