xml parser data to a text file

i am going through the code for xml parser, and i found this code, but i want to save result data to a text file.

public class ReadXMLFile 
{

public static void main(String argv[]) 
{

  try {

    File fXmlFile = new File("test.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    doc.getDocumentElement().normalize();

    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
    NodeList nList = doc.getElementsByTagName("test");
    System.out.println("-----------------------");

    for (int temp = 0; temp < nList.getLength(); temp++) {

       Node nNode = nList.item(temp);
       if (nNode.getNodeType() == Node.ELEMENT_NODE) {

          Element eElement = (Element) nNode;

          System.out.println("ID : " + getTagValue("id", eElement));
          System.out.println("Name : " + getTagValue("Name", eElement));
          System.out.println("AGE: " + getTagValue("age", eElement));

       }
    }
  } 
  catch (Exception e) 
  {
    e.printStackTrace();
  }
 }

 private static String getTagValue(String sTag, Element eElement) 
{
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

    Node nValue = (Node) nlList.item(0);

return nValue.getNodeValue();
}

}

how can i save the output to a text file ?

please suggest me regarding this.......

Answers


Create a Writer to write to a file and replace System.out.println() with Writer.write().

BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(s);
writer.newLine();

Need Your Help

ios: Application tried to present a nil modal view controller on target

ios7 mfmailcomposeviewcontroller

I am developing an application,the requirement is to open email composer on a button click of UIAlertView.

Hitting breakpoints in MonoDevelop 2.2 Beta 1 on OSX

macos breakpoints monodevelop

Just installed MonoDevelop 2.2 Beta 1 on OSX Snow Leopard, and all is good.