2

I was trying to modify an xml file by using DOM and this happened:

javax.xml.transform.TransformerException: java.io.FileNotFoundException: file:\D:\myproject\build\web\xml\myFile.xml (The filename, directory name, or volume label syntax is incorrect)
        at org.apache.xalan.transformer.TransformerIdentityImpl.createResultContentHandler(TransformerIdentityImpl.java:263)
        at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:296)
        at utils.UpdateUtils.BookUpdate(UpdateUtils.java:36)

This is my code

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();

        Document doc = db.parse(f);
        searchAndModify(doc); //modify xml's contents

        Source source = new DOMSource(doc);
        Result result = new StreamResult(f);
        TransformerFactory tff = TransformerFactory.newInstance();
        Transformer trans = tff.newTransformer();
        trans.transform(source, result);

f is my xml, generated. It parsed to Document doc just fine. However, when transformed, exception was thrown.

I've tried to parse to a new xml, same folder, but to no avail:

Result result = new StreamResult(new File(path, "newFile.xml"));

javax.xml.transform.TransformerException: java.io.FileNotFoundException: file:\D:\myProject\build\web\newFile.xml (The filename, directory name, or volume label syntax is incorrect)

Anyone has encountered this problem or has a solution for it? Please help me!

1
  • See this link for correct answer here ... [Correct Answer for XML parsing ][1] [1]: stackoverflow.com/questions/15432145/… Commented Mar 13, 2014 at 9:20

4 Answers 4

3

StreamResult result = new StreamResult(new File(filepath).getAbsolutePath());

Is working for the file not found exception.

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

Comments

1

It appears the problem is here:

java.io.FileNotFoundException: file:\D:\myProject\build\web\newFile.xml

It looks to me like your file name starts with the six characters file:\. Make sure your file name starts with D:.

If you happen to prefer using a URL over using a file name, be aware that the above is not a valid URL, because URLs are required to use forward slashes (/) on all platforms.

2 Comments

Actually the url is fine. Browser recognizes url that way.
It is not clear from your code what the type of f is, but since you are getting a FileNotFoundException, I'm guessing it is a File object. A File object should not contain "file:" in it. If f is a URL, then I would advise using the forward slashes. Browsers are known for being highly fault-tolerant, and the java.net.URL class is also pretty fault-tolerant, but converting that URL to a File may very well cause a FileNotFoundException.
0

You should provide correct file path for your xml file. seems you are trying to access a file in your web application, so you can specify your application resource path in web.xml

<context-param>
  <param-name>xmlPath</param-name>
  <param-value>D:\somefolder\</param-value>
</context-param>

then use

String xmlFilePath = new File(e.getServletContext().getInitParameter("xmlPath"));
File customerDataFile = new File(xmlFilePath , "newFile.xml");

2 Comments

print customerDataFile.getAbsolutePath() and check this what u r getting
if error due to \ try <param-value>D:/somefolder/</param-value>
0

I came across this same issue and I believe I know what it is.

For me I had:

  // Make a string stream out of the xml.
  Source source = new StreamSource(new ByteArrayInputStream(xml.toString().getBytes("UTF-8")));
  FileOutputStream stream = new FileOutputStream(file);
  StreamResult result = new StreamResult(stream);
  transformer.transform(source, result);
  stream.close();

which worked fine

I then polished this with

  // Make a string stream out of the xml.
  Source source = new StreamSource(new ByteArrayInputStream(xml.toString().getBytes("UTF-8")));
  // Transform it straight into the output file.
  StreamResult result = new StreamResult(file);
  transformer.transform(source, result);

which also worked fine on my development machine.

Unfortunately it then broke when I installed this on site. It transpired that the version of xalan on site was older than my test setup. It appears that older versions of xalan had problems writing directly to Files.

It seems @VGR's idea was close.

The subtle point to notice is the file:\D:\... should actually read file:/D:/... which is the problem with earlier versions of xalan.

1 Comment

I see a fix that looks like it is intended to fix this issue.

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.