Writing the data to an XML file


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Writing the data to an XML file

Hybrid View

  1. #1
    Join Date
    Jul 2005
    Posts
    1

    Writing the data to an XML file

    Hi,

    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.

    Thanks

    Jedeye
    Attached Files Attached Files

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    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:
    Code:
    PrintWriter xmlOutput = new PrintWriter(new FileWriter(new File("ouput.xml")));
    
    xmlOutput.println("<root-element>");
    // ... Other printing code goes here ...
    xmlOutput.println("</root-element>");
    This is generally pretty simple and should work for you. A few things to remember:
    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.
    ~evlich

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center