Hi Kalyani, I'm pretty new to XML myself. But what you are describing sounds like it might be an appropriate application for "DOM" (Document Object Model).
Check out the Java API for the org.w3c.dom and org.w3c.dom.ls packages. Though I've never used the org.w3c.dom.ls.LSSerializer to output XML, it seems like a good candidate. Besided this, there are lots and lots of other ways to take an in-memory representation of XML data (a DOM) and serialize it to disk.