Sunday, April 27, 2008

Reading and Writing XML file using JAVA

the purpose of program :

* Read the contents of file Books.xml
* Copy the contents to New_Books.xml

Desired output

New_Books.xml with same contents as Books.xml

here is Books.xml
-----------------------------------------------------------------
Jeffery ArcherProdigal Daughter$39Richard BransonLoosing My Virginity$19Paulo CoheloVeronica Decides to Die$23
-----------------------------------------------------------------

/*
ParsingXML_DOM.java

This program demostrates
• Parsing an XML file Books.xml
• Copying the contents of Books.xml to New_Book.xml



Compiled By:
Kaustubh Verma
www.campusfever.wordpress.com
www.javagurug.blogspot.com

*/
package Parsing_DOM;

import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;

public class ParsingXML_DOM {

public ParsingXML_DOM() {

}

public static void main(String []args)throws Exception{

Document document = null;

document = DOMUtil.parse("Books.xml");

if (document != null){
// Print XML content

DOMUtil.printDOM(document);

// Write to a new file
DOMUtil.writeXmlToFile("New_Books.xml",document);

}
}





}

get the code of DOMUtil here

No comments: