File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes how to write the data from text fields of my swing application to an xml file dynamically? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to write the data from text fields of my swing application to an xml file dynamically?" Watch "how to write the data from text fields of my swing application to an xml file dynamically?" New topic
Author

how to write the data from text fields of my swing application to an xml file dynamically?

Pooja Oza
Greenhorn

Joined: Apr 01, 2009
Posts: 21
hi,

can some one help me how can i save the values of the text fields from my swing desktop application to an XML file dynamically on the click of a submit button in a window?

Please i really do not know what to do, so please help me friends.............


Thanks,

pooja

Thanks,

Pooja Oza
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
Two things. First, look into placing an Action Listener on the button. This will call your method when the user clicks the button.
Next, look into the various java.io.* classes, like FileOutputStream for example.

If you want help writing the file in XML format, there are packages that make this easy. Although I've used these a few times, I can't remember the name of one off-hand, so perhaps someone else will provide that. You can just write the file yourself, formatting it as XML manually, of course.
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
Usually you'd serialize a Document using a Transformer ...

... or using JDOM a XMLOutputter ...

... or using Xerces XMLSerializer ...

... or something similar

Of course, usually you would first of all define a format for the XML document. Then maybe creating a new Document on a button action and reading the desired text(s) from the components, adding elements / attributes to the document. Maybe Pooja could show us some code he already has, or at least explain in detail where the problem is located?


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Pooja Oza
Greenhorn

Joined: Apr 01, 2009
Posts: 21
Thanks Marc and Peter.

Peter as you said, if this is my code, and i want to store the textfields mentioned in the code below in an xml file when a button is clicked, so how can i do that? can you please guide me with this.......

Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
I think you're missing something in the use of the Action Listener on your buttons. There are tutorials on the use of Action Listeners, just google for them. Here's one: http://java.sun.com/docs/books/tutorial/uiswing/components/button.html

Hint: You're not adding the action listener to your button, so the actionPerformed method will never be called.
Pooja Oza
Greenhorn

Joined: Apr 01, 2009
Posts: 21
Thanks Marc for pointing that button listener thing. I made that changes to my code, then how can i proceed further? Do you have any idea?

I have four text fields, the user enters his details, then when he clicks the submit button, the details should be stored in an xml file, so how can i do that? and also in case i want to retrieve data from xml file then what can be done? I am completely blank in xml so do not know what to do? can you give me some guidance?

Thanks,
Pooja
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
XML is just a way to format/structure text. Do you know how you want the XML formatted? You will need to know this.
Then, in your actionPerformed() method, just collect the data from the fields, and write it to the file.

I suspect what you're wanting someone to tell you is how to structure the XML file, but XML is structured however you want it.
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
Two examples. The first one outputs an XML document using a org.w3c.dom.Document and the second one simply uses java.lang.StringBuilder. You can see that for real simple data there's not always need for the overhead of 'real' XML processing.



Both buttons read the contents of two textfields and ouput them into a document in the form of:

Pooja Oza
Greenhorn

Joined: Apr 01, 2009
Posts: 21
Thanks Peter and Marc. Luckily I too figured out a way to do this task. I simply did this with the following code- you can have a look.
Thanks once again to both of you, as you both guided me so that I was able to know something about XML atleast, but ya this DOM and Transformer stuff, i really need to focus on as i am new to all this. You both have been of great help!!! Thanks once again.



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to write the data from text fields of my swing application to an xml file dynamically?
 
Similar Threads
sending xml file from server to client dynamically.
how to save as XML with some predefined extensions
How to use java.awt.Image with HTML?
How to i embed Flash file in the Swing Application
How to get value from dynamic textfield