Writing the data to an XML file
I am very new to Java and have been writing a very small basic program for college, but I have got stuck with writing the data my program outputs, into an XML file.
Basically I want the data calculated to be output into an XML file which will be formatted to look like a quotation, and then read by the program, although I am only bothered about the writing at the moment.
The code for the program is in the attached text file. It would be very helpful if someone could tell me where to start with writing XML for this program, or point me in the direction of a good tutorial.
I have sofar tried several examples on the net, like OutputStream, but with no luck. I think this may be because I don't really know how to do it properly.
There are two ways to write XML output. The first is simply to write text that is formatted like XML, the second is to use one of the several XML libraries to create the document. The benefit that you get from the first is that it is really easy to do things if you already know things about your document. The benefit of the second is that you can read from you document as you write and compute things on the fly.
Personally I would recommend the first choice. In that case you would just use a print writer. For example:
This is generally pretty simple and should work for you. A few things to remember:
PrintWriter xmlOutput = new PrintWriter(new FileWriter(new File("ouput.xml")));
// ... Other printing code goes here ...
1) escape special characters (<, >, ", etc) when writing data out. http://www.javapractices.com/Topic96.cjp
2) Close all elements
If you do it right, your ouput should be fine. If you're having problems, post some of the output code up. Hope this helps.
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center