Solved

Pound sign in FileName - When saving an xml document using Transformer and StreamResult

Posted on 2011-09-08
11
437 Views
Last Modified: 2012-06-22
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.
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();
	   }
		
	}

}

Open in new window

0
Comment
Question by:infinidem
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36504727
Are you sure it's not more to do with the space?
0
 

Author Comment

by:infinidem
ID: 36504759
positive - "Test White Space.xml" is fine. while "Test White Space #2.xml" will be written to disk as "Test White Space"
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36504810
On which OS?
0
 

Author Comment

by:infinidem
ID: 36504845
Windows 7 64 bit.
0
 

Author Comment

by:infinidem
ID: 36504881
also on Mac OSX (10.6.8)
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36504969
this seeems to be something similar to your problem in TransfioomFactory or something
http://stackoverflow.com/questions/1858221/pound-sign-in-directory-name-in-java-how-to-read-it



pound sign may be a problem in the filename when it is used in the link on the web
(as poud is used to point to some location on the pag)
0
 
LVL 16

Expert Comment

by:Bryan Butler
ID: 36505464
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.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36505498
yes, and though they say there is a workarond there it seems that workaround is - don't use pound
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36506329
>>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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36506514
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);

Open in new window

0
 

Author Comment

by:infinidem
ID: 36506695
But the link to the big  within that page is.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now