infinidem
asked on
Pound sign in FileName - When saving an xml document using Transformer and StreamResult
Ran into a percular problem today. When saving an xml file that has a '#' character in it the file is save up to the point of the hash. For example. 'Test #2.xml' would be saved as 'Test'.
I created a short program below to demonstrate the problem.
I created a short program below to demonstrate the problem.
import java.io.File;
import javax.swing.JFileChooser;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class saveFile {
public static void main(String[] args) {
try {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Save Project");
chooser.setAcceptAllFileFilterUsed(false);
File file = null;
if(chooser.showDialog(null, "Save") == JFileChooser.APPROVE_OPTION){
file = chooser.getSelectedFile();
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element project = doc.createElement("project");
project.setAttribute("title", "test");
doc.appendChild(project);
Source source = new DOMSource(doc);
System.out.println(file.getAbsolutePath());
Result result = new StreamResult(file);
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(source, result);
}
} catch (Exception ex){
ex.printStackTrace();
}
}
}
Are you sure it's not more to do with the space?
ASKER
positive - "Test White Space.xml" is fine. while "Test White Space #2.xml" will be written to disk as "Test White Space"
On which OS?
ASKER
Windows 7 64 bit.
ASKER
also on Mac OSX (10.6.8)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
for_yan is right. His link has this link in it which explains the issue:
http://bugs.sun.com/bugdatabase/view_bug.do?bug%5Fid=4294586
And seaching for "java "pound sign" path bug", it looks like it's still a problem 10+ years later.
http://bugs.sun.com/bugdatabase/view_bug.do?bug%5Fid=4294586
And seaching for "java "pound sign" path bug", it looks like it's still a problem 10+ years later.
yes, and though they say there is a workarond there it seems that workaround is - don't use pound
>>http://stackoverflow.com/questions/1858221/pound-sign-in-directory-name-in-java-how-to-read-it
is not relevant, as
a. the StreamResult ctor is the one in question here, not StreamSource
b. the parameter is of type File anyway, not String
is not relevant, as
a. the StreamResult ctor is the one in question here, not StreamSource
b. the parameter is of type File anyway, not String
In fact, if you do the very reverse of what is suggested at the link that for_yan posted (use String and not File), you'll probably have not problem (i don't)
Result result = new StreamResult(file.getAbsolutePath());
Transformer xformer = TransformerFactory.newInstance()
.newTransformer();
xformer.transform(source, result);
ASKER
But the link to the big within that page is.