Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Write an xml dom to a xml file

  1. #1
    Junior Member Kakashi's Avatar
    Join Date
    Oct 2009
    Posts
    29
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Write an xml dom to a xml file

    I have got the xml to print in the system, but I want to have that printed into a .xml file, how do I do that? Here is what I got so far
    import javax.xml.stream.*;
     
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.w3c.dom.Document;
    import java.io.*;
     
    public class XMLWriter {
     
     
       public static void main(String[] args) throws XMLStreamException  {
     
       File xmlFile = new File("file.xml");
       try{
           Document document;
           FileWriter fstream = new FileWriter(xmlFile);
           BufferedWriter out = new BufferedWriter(fstream);
           DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
           DocumentBuilder builder = factory.newDocumentBuilder();
     
          // Create an output factory
          XMLOutputFactory xmlof = XMLOutputFactory.newInstance();
     
          // Create an XML stream writer
          XMLStreamWriter xmlw = xmlof.createXMLStreamWriter(System.out);
     
          xmlw.writeStartDocument();
          xmlw.writeDTD("<!DOCTYPE file SYSTEM \"file.dtd\">");
          xmlw.writeStartElement("file");
          xmlw.writeEmptyElement("filename");
          xmlw.writeAttribute("filename", "ReadMe.txt");
          xmlw.writeEmptyElement("keyword");
          xmlw.writeAttribute("keyword", "Some word to match");
          xmlw.writeEmptyElement("option");
          xmlw.writeAttribute("option", "matchcase");
     
     
          // Write document end. This closes all open structures
          xmlw.writeEndDocument();
          // Close the writer to flush the output
     
          xmlw.close();
           }catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
        }
     
       }
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Write an xml dom to a xml file

    See a) the API for XMLOutputFactory - the method createXMLStreamWriter takes an OutputStream to write to (currently you are using System.out) b) See FileOutputStream (Java Platform SE 6). In other words, create a FileOutputStream and pass this instead of System.out

  3. The Following User Says Thank You to copeg For This Useful Post:

    Kakashi (March 2nd, 2011)

  4. #3
    Junior Member Kakashi's Avatar
    Join Date
    Oct 2009
    Posts
    29
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Write an xml dom to a xml file

    Ah perfect thank you for that it worked fine all I had to do was this
    XMLStreamWriter xmlw = xmlof.createXMLStreamWriter(new FileOutputStream("file.xml"));
    and all better

Similar Threads

  1. Writing in a file using Java
    By JavaPF in forum File Input/Output Tutorials
    Replies: 4
    Last Post: December 17th, 2011, 04:33 PM
  2. Any way to write html form data to file?
    By nathan.fortier@gmail.com in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 14th, 2011, 03:03 PM
  3. Write ascii in a specific position of the file
    By generalitico in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: November 11th, 2010, 04:40 AM
  4. [SOLVED] Why can't I write to file inside a doGet() method?
    By FailMouse in forum Java Servlet
    Replies: 1
    Last Post: July 7th, 2010, 01:15 AM
  5. write text to a file help
    By wolfgar in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: November 24th, 2009, 08:36 AM