• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java to xml how to ??

 
madhu cm
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, i have some fields in swings apps like username and password...... so i need to generate an XML file using that file. can you please tell me how to generate an xml file?? which is better way to use either DOM or SAX ??

just i want to generate an xml doc from java source.....

help me, thank you
 
kirtikumar patel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!madhu
you can you xml schema for it.


kirti
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a number of ways to do this.
DOM is a possibility (generate the DOM tree in memory and then serialize it to a file). If it's just a few elements like you suggest, you could just write the XML directly to a file. I've used XOM (http://www.xom.nu/) in the past to do this instead of DOM, but that's personal preference.
(SAX is an XML parsing method, so that isn't going to help you. XML schema is a description of the structure of an XML file, which won't do you any good either.)
 
Joe Ess
Bartender
Posts: 9318
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Madhu. We try to keep things friendly around here so we have a name policy. Basically we ask that everyone use a first name and a last name seperated by a space and not use a name that is obviously fake.
About your question, XML isn't as complex as it seems. It's just ASCII text with some markup around the data to indicate what the data is. There's tutorials on Java's API's for DOM and SAX in the J2EE Tutorial. DOM is usually used to manipulate an XML document and SAX is used to trigger events using an XML document (there's a more in depth discussion in the tutorial).
If you are just writing out a simple XML document, you may want to skip the API's and just write the markup and data to your output stream:

Note that XML must be "well formed", meaning every "open" tag (i.e. "<user>") must have a corresponding "close" tag (i.e. "</user>")
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic