Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2011-09-08
Medium Priority
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");
			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");
				Source source = new DOMSource(doc);
				Result result = new StreamResult(file);
				Transformer xformer = TransformerFactory.newInstance().newTransformer();
				xformer.transform(source, result);
	   } catch (Exception ex){


Open in new window

Question by:infinidem
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
LVL 86

Expert Comment

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

Author Comment

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

Expert Comment

ID: 36504810
On which OS?
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Author Comment

ID: 36504845
Windows 7 64 bit.

Author Comment

ID: 36504881
also on Mac OSX (10.6.8)
LVL 47

Accepted Solution

for_yan earned 2000 total points
ID: 36504969
this seeems to be something similar to your problem in TransfioomFactory or something

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)
LVL 16

Expert Comment

by:Bryan Butler
ID: 36505464
for_yan is right.  His link has this link in it which explains the issue:


And seaching for "java "pound sign" path bug", it looks like it's still a problem 10+ years later.
LVL 47

Expert Comment

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

Expert Comment

ID: 36506329

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
LVL 86

Expert Comment

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()
                xformer.transform(source, result);

Open in new window


Author Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

618 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