Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

xml - java.net.URISyntaxException

I have get this exception. but this exception is not reproduced again. I want to get the cause of this

Exception Caught while Checking tag in XMLjava.net.URISyntaxException:
Illegal character in opaque part at index 2:
C:Documents and SettingsAll Users.SFconfigsd.xml
stacktrace net.sf.saxon.trans.XPathException.

Why this exception occured. How to deal with so it will not reproduce.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A valid URI does not contain backslashes, and if it contains a : then the characters before the first : must be a "protocol".

Basically "C:Documents and SettingsAll Users.SFconfigsd.xml" is a pathname, and not a valid URI.

If you want to turn a pathname into a "file:" URI, then do the following:

File f = new File("C:Documents and SettingsAll Users.SFconfigsd.xml");
URI u = f.toURI();

This is the simplest, and most reliable and portable way to turn a pathname into a valid URI in Java. It should work on Windows, Mac, Linux and any other platform that supports Java. (Other solutions that involve using string bashing on a pathname are not portable.)

But you need to realize that "file:" URIs have a number of caveats, as described in the javadocs for the File.toURI() method. For example, a "file:" URI created on one machine usually denotes a different resource (or no resource at all) on another machine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...