9

i am facing some problem for this following codes

       try {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

            //root elements
            Document doc = docBuilder.newDocument();

            Element rootElement = doc.createElement("subcompany");
            doc.appendChild(rootElement);

            //id elements
            Element id = doc.createElement("id");
            id.appendChild(doc.createTextNode(subCompanyId != null ? subCompanyId : " "));
            rootElement.appendChild(id);

            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);

            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

            String xmlPath = "/project/MallDirectory/mall";

            //EDITED for File creation before writing.
            boolean isFileCreated = new File(xmlPath, "subcompany.xml").createNewFile();
            System.out.println(isFileCreated);                

            StreamResult result = new StreamResult(new File(xmlPath, "subcompany.xml"));

            transformer.transform(source, result);

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

After i run, i get this following error:

javax.xml.transform.TransformerException: java.io.FileNotFoundException: file:/project/MallDirectory/mall/subcompany.xml (No such file or directory)

It used to work on my other project, however not this time round. What exactly went wrong here ?

EDITED:

Here is the path that i am trying to write into. The file is created, but it is empty.

enter image description here

5
  • 2
    Is there a file at "/project/MallDirectory/mall/subcompany.xml"? Commented Mar 10, 2013 at 15:40
  • Where is the file located? Commented Mar 10, 2013 at 15:40
  • Is /project is your project name ? Commented Mar 10, 2013 at 15:41
  • a FileNotFoundException when writing usually suggests that the directory into which you are trying to write (in this case /project/MallDirectory/mall) does not exist. Commented Mar 10, 2013 at 15:43
  • /project is a directory. for testing, i created the subcompany.xml before executing the transformer.transform. the file is created, however it is empty and same error. Commented Mar 10, 2013 at 15:45

4 Answers 4

22

I managed to solve the problem.

Here is the Error:

javax.xml.transform.TransformerException: java.io.FileNotFoundException: file:/project/MallDirectory/mall/subcompany.xml (No such file or directory)

I am thinking maybe the transformer is trying to write the xml to this path 'file:/project/MallDirectory/mall/subcompany.xml'. I don't know how it happened, as i have specifically set the file path '/project/MallDirectory/mall/subcompany.xml', and does not prefixed with 'file:/'.

Therefore, i somehow managed to fix it by doing this:

...

//ERROR CODE:
//StreamResult result = new StreamResult(new File(xmlPath, "subcompany.xml"));
//
StreamResult result = new StreamResult(new File(xmlPath, "subcompany.xml").getPath());
transformer.transform(source, result);

...
Sign up to request clarification or add additional context in comments.

2 Comments

Charlie - thanks so much for posting this. I am experiencing the same problem. Do you have any idea why we have to use .getPath() ? It defies any logic I can muster! Thanks!
I had the same, I think StreamResult constructor corrupts file path, it shoul start with "file://" for example "file:///tmp/lol". In your case it's starting with "file:", anyway thank you, getPath() did well.
0

The number of files that can be in open state at any point of time is specific to the OS (offcourse ,Can be configured) and you have reached the upper limit of that.Look in the code base of your application ,whehter you have some code that is trying to open a file but not closing the stream after its use.Check for such codes.

Comments

0

The directory path you have defined is incorrect. Take a look at the JavaDoc to determine which form of the directory path you need to get to your file location.

JavaDoc java.io.File

1 Comment

yes, somehow my machine is pointing to the different path. maybe because of my machines OS file system.
0

If "/project" is your project name then try "./MallDirectory/mall" else try "./project/MallDirectory/mall". Please observe the string carefully it contains dot in it.

6 Comments

Hi, /project is a directory to store all my application config files. my netbeans project resides in different directory.
Can you just provide directory structure? So, it will be much easy to explain what is happening?
sure. i am trying to write a xml file inside /project/MallDirectory/mall folder. /project is located on my machine root folder (Machintosh HD)
I don't have much knowledge Machintosh HD file system. You can give a try for copy the whole path from finder and put it in your code. Let m know it worked or not :)
@CharlieKee: The code works fine on windows m/c :) as I replace your path with drive path. Delete that file,get the full path by right clicking on mall folder then on getInfo, which wil give you the whole path
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.