3 Replies - 4046 Views - Last Post: 01 February 2012 - 03:29 PM Rate Topic: -----

#1 acll19  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 20-October 10

Transforming an xml file issue

Posted 18 January 2012 - 09:00 PM

Hi, there. I'm making an aplication with java that reads a configuration file when it gets started and if it is the first time it asks the user for some data. The thing is I'm using an xml file to do that and I'm getting this error when I call the method save:

java.io.FileNotFoundException:file:... (The file name, the directory name or the volume tag sintax are not correct)

the funny thing is that I wrote the code I use to modify the file in a simple console application and it runs just fine.

here's the code I use:
public void save() throws XMLStreamException,
            FileNotFoundException, TransformerConfigurationException,
            TransformerException, Exception {


        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");


        File f = new File(cnflocation);//cnflocation is the location of the configuration file
        if (f.exists()) {
            StreamResult result = new StreamResult(f);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);// here's where the Exception gets thrown
        } else {
            throw new Exception("The file doesn't exist");
        }

    }





PS: I read the data on that file just fine so the file exist in that loacation.

*Edited: code tags fixed

This post has been edited by pbl: 18 January 2012 - 09:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Transforming an xml file issue

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1428
  • View blog
  • Posts: 3,172
  • Joined: 05-April 11

Re: Transforming an xml file issue

Posted 19 January 2012 - 06:39 AM

Can you show what your cnflocation and doc looks like :) ?
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8037
  • View blog
  • Posts: 13,754
  • Joined: 19-March 11

Re: Transforming an xml file issue

Posted 19 January 2012 - 09:25 AM

I'm especially curious about doc, myself. Since file.exists() returns true, it seems likely that doc is the problem, no?

The obvious diagnostic would be to write a test() method that opens each file and spits out what it finds.
Was This Post Helpful? 0
  • +
  • -

#4 acll19  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 20-October 10

Re: Transforming an xml file issue

Posted 01 February 2012 - 03:29 PM

I'm sorry I posted this so late, I was having some trouble with my internet.

Anyway I got to fix the problem. I don't really know why it got fixed, I just created a StreamWriter and passed ti to the StreamResult constructor and it worked.

Here's the fixed code.

Thanks for your help.

if (f.exists()) {
	    StreamWriter sw = new StreamWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);// here's where the Exception gets thrown
        } else {
            throw new Exception("The file doesn't exist");
        }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1