Results 1 to 9 of 9
  1. #1
    mvg's Avatar
    mvg
    mvg is offline Member
    Join Date
    Oct 2008
    Posts
    23
    Rep Power
    0

    Default [SOLVED] Save an xml file

    Hello Everybody,

    I want to know how to save an xml file using java. I searched google and found only code to read the xml file and save without parsers like .txt files. Is there any method to save xml file using parsers? Please help

    Thank you,
    mvg
    Last edited by mvg; 10-10-2008 at 01:49 PM.

  2. #2
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    Quote Originally Posted by mvg View Post
    Hello Everybody,

    I want to know how to save an xml file using java. I searched google and found only code to read the xml file and save without parsers like .txt files. Is there any method to save xml file using parsers? Please help

    Thank you,
    mvg
    mvg,

    Yes you can save data to XML and load it back using either:
    • DOM (document object model)
    • SAX (simple API for XML)


    DOM retains the XML as a data structure in memory, you can maniuplate it. SAX is event-based, and a bit more complex to work with.

    I write to and from XML files and normally use SAX.

    I suggest you look up these terms and if you have more specific questions you can always come back here.

  3. #3
    mvg's Avatar
    mvg
    mvg is offline Member
    Join Date
    Oct 2008
    Posts
    23
    Rep Power
    0

    Default save a xml file

    Thank you Paul,

    Thank you for the answer, but when I searched through the net I got only to generate a xml file and to read it. Sorry for my ignorance. I think I am missing something here. Can you suggest me a good tutorial regarding this issue?

    Thank you,
    Girish

  4. #4
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    Quote Originally Posted by mvg View Post
    Thank you Paul,

    Thank you for the answer, but when I searched through the net I got only to generate a xml file and to read it. Sorry for my ignorance. I think I am missing something here. Can you suggest me a good tutorial regarding this issue?

    Thank you,
    Girish
    Girish,

    What exactly is your problem? Have you got so far with a program and can't go any further? I thought you were asking how to generate an XML file, you talk about saving? What are you saving? Please explain.

    I do have a sample app which takes objects in java and generates and saves an XML file, and can do the reverse - read XML files in, and create the objects in memory again. If you let me know what your email address is I could give you it.

  5. #5
    mvg's Avatar
    mvg
    mvg is offline Member
    Join Date
    Oct 2008
    Posts
    23
    Rep Power
    0

    Default Save an xml file

    Thank you paul,

    I am unable to post the email id now. When I did that I got the warning message as

    "To be able to post e-mail addresses your post count must be 20 or greater. You currently have 3 posts.

    Please remove e-mail addresses from your message, then you will be able to submit your post."

    I want to save the xml code generated into a seperate file like "filename.xml"

    If possible can you post the link containing the sample code or can you post the code in forums?

    Thanks for the help in advance

    Girish
    Last edited by mvg; 10-13-2008 at 06:08 AM.

  6. #6
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    You say 'the code generated' ... are you saying you have already generated the code, and if so, in which form is it?


    The code is too large to post here, and I have the same problem about email addresses.

  7. #7
    mvg's Avatar
    mvg
    mvg is offline Member
    Join Date
    Oct 2008
    Posts
    23
    Rep Power
    0

    Default Save a XML file

    Paul,

    I am able to print the XML code in console. I am searching for a way to save the code in seperate xml file say 'product.xml'.

    I am new to Java programming. So please forgive me if my question appears silly.

    Thanks in advance,
    Girish

  8. #8
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    Quote Originally Posted by mvg View Post
    Paul,

    I am able to print the XML code in console. I am searching for a way to save the code in seperate xml file say 'product.xml'.

    I am new to Java programming. So please forgive me if my question appears silly.

    Thanks in advance,
    Girish
    No, its not silly at all Girish, I remember I was asking these questions myself not so long ago. Here is the sort of thing you need:

    Java Code:
    String xmlcode;
    
    //you need to put your code here that puts the xml into this variable, or you can use your own variable and replace the name below
    
    
    
    
    //create the objects necessary for writing to a file
    File xmlfile= new File("C:\\myfile.xml");
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter(xmlfile));  
    }
    catch (IOException e) {
        e.printStackTrace();
    } 
    
    
    //this is the bit where you write your XML to the file
    try {
        writer.write(xmlcode);
    }
    catch(IOException e) {
        e.printStackTrace();
    }
    
    //close the file (this is important)
    try {
        writer.close();
    }
    catch(IOException e) {
        e.printStackTrace();
    }

  9. #9
    mvg's Avatar
    mvg
    mvg is offline Member
    Join Date
    Oct 2008
    Posts
    23
    Rep Power
    0

    Default save an XML file

    Thank you Paul,

    Thank you for your reply. It helped me a lot. Just an information, there is a option here to attach files with the post. You can use it to post large codes.

    Girish
    Last edited by mvg; 10-13-2008 at 12:31 PM.

Similar Threads

  1. Read file from URL and save to FTP
    By nitinmukesh in forum Networking
    Replies: 6
    Last Post: 06-03-2012, 08:45 PM
  2. upload and save file
    By chennee72 in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 06-29-2011, 02:27 PM
  3. How to Save/Load Vector to/from file
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:37 PM
  4. save file to database
    By katerinaaa in forum New To Java
    Replies: 0
    Last Post: 08-14-2007, 12:15 PM
  5. Replies: 1
    Last Post: 08-07-2007, 05:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •