tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 XML > XOM > Serialize to File

Serialize to File 

XOM (XML Object Model) is a tree based java API for processing XML by taking the best ideas from SAX and DOM. It is simple, fast and easy to use. This requires the library xom-1.2.7.jar to be in classpath. The following example shows serializing an XML document to a file.

File Name  :  
com/bethecoder/tutorials/xom/tests/SerializeToFile.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.xom.tests;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Serializer;

public class SerializeToFile {

  /**
   @param args
   @throws IOException 
   */
  public static void main(String[] argsthrows IOException {
    Document doc = new Document(new Element("RandomNumbers"));
    Random random = new Random();
    Element ranEle = null;
    for (int i = ; i < 10 ; i ++) {
      ranEle = new Element("Number");
      ranEle.appendChild(String.valueOf(random.nextInt(1729)));
      doc.getRootElement().appendChild(ranEle);
    }
    
    //Indent the XML document and Serialize to file
    OutputStream outs = new FileOutputStream("randoms.xml");
    Serializer serializer = new Serializer(outs, "UTF-8");
    serializer.setIndent(4);
    serializer.write(doc);
    serializer.flush();
  }

}
   

It gives the following output,
File Name  :  randoms.xml



 
  


  
bl  br