View Single Post
  #1   (View Single Post)  
Old 14th October 2009
c0mrade's Avatar
c0mrade c0mrade is offline
Port Guard
Join Date: May 2008
Posts: 41
Default output to a file in java

I wanna remove element from xml file, when I run the following code I get the correct output when I'm outputing to console but when I'm trying to ouptup the same code to a file I get the same / original xml file without removed element, here is the code :


import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.*; 
import javax.xml.transform.dom.DOMSource; 

public class RemoveBlock {

  static public void main(String[] arg) {
      String xmlFile = "Management.xml";
      File file = new File(xmlFile);
      String remElement = "Physical_Order_List_Array";
      if (file.exists()){
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer tFormer = tFactory.newTransformer();
        Element element = (Element)doc.getElementsByTagName(remElement).item(0);
//        Remove the node
//              Normalize the DOM tree to combine all adjacent nodes
        Source source = new DOMSource(doc);
        Result dest = new StreamResult(System.out);
        tFormer.transform(source, dest);
       // System.out.println();
       // saveStringToFile("emir.xml", "dsds"); -> write method
        System.out.println("File not found!");
    catch (Exception e){
The code above is working but when I want to change it to output to a file like : Result dest = new StreamResult(new File("foo.xml")); or Result dest = new StreamResult(new FileOutputStream("foo.xml")); its not working, any indications why ?

Thank you
Reply With Quote