35

I have a xml URL file in which there are white spaces i want to replace white spaces with %20.. how to do this????

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();

/** Send URL to parse XML Tags */
URL sourceUrl = new URL(
                "http://www.arteonline.mobi/iphone/output.php?gallery=MALBA%20-%20MUSEO%20DE%20ARTE%20LATINOAMERICANO%20DE%20BUENOS%20AIRES");

XMLHandlerartistspace myXMLHandler = new XMLHandlerartistspace();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
Robert Massaioli
  • 13,379
  • 7
  • 57
  • 73
SRam
  • 2,832
  • 4
  • 45
  • 72

7 Answers7

55

Try this:

String temp = http://www.arteonline.mobi/iphone/output.php?gallery=MALBA%20-%20MUSEO%20DE%20ARTE%20LATINOAMERICANO%20DE%20BUENOS%20AIRES

temp = temp.replaceAll(" ", "%20");
URL sourceUrl = new URL(temp);
HighLife
  • 4,218
  • 7
  • 40
  • 56
Sumant
  • 2,775
  • 2
  • 22
  • 30
  • 11
    I think this is the wrong approach. The URL (excluding the host part) should be URL-encoded (using `URLEncoder.encode`). After doing that, you can replace the `+` characters with `%20`. If you don't use `URLEncoder.encode`, then there's the risk that other special characters in the URL might cause problems. (Note that `+` to represent a space character is perfectly valid in a URL; you might not even need `%20`.) – Ted Hopp Aug 19 '13 at 21:33
  • Do note that URI.Builder solution below is correct. This gets the job done.. but in a professional setting you want the solution from SudoCode – StarWind0 Mar 01 '16 at 03:58
35

When you build your URL you should use URLEncoder to encode the parameters.

StringBuilder query = new StringBuilder();
query.append("gallery=");
query.append(URLEncoder.encode(value, "UTF-8"));

If you already have the whole URL in a String or a java.net.URL, you could grab the query part and rebuild while URLEncoding each parameter value.

ewan.chalmers
  • 16,145
  • 43
  • 60
  • 1
    This is the right way. If you are using a regex yourself or replaceAll then you are doing it wrong. – Robert Massaioli May 18 '11 at 13:28
  • But be sure that you dont URL encode the host part of the url and the '/' characters inside the url. try a substring of the complete url or do it in the build process as sudocode suggested with a string builder. – DArkO May 18 '11 at 13:30
  • 8
    If for some reason OP can't live with `+` representing spaces and really needs `%20`, it's easy enough to call `replace("+", "%20")` after calling `URLEncoder.encode`. – Ted Hopp Aug 19 '13 at 21:34
  • why `query.append("gallery=");` ? – Muhammed Refaat Dec 05 '18 at 19:48
  • 1
    @MuhammedRefaat the parameter name and equals sign do not need to be URL encoded. The parameter value should be. – ewan.chalmers Dec 10 '18 at 16:53
22

Just one addition to sudocode's response:

Use android.net.Uri.encode instead of URLEncoder.encode to avoid the "spaces getting converted into +" problem. Then you get rid of the String.replaceAll() and it's more elegant :)

StringBuilder query = new StringBuilder();
query.append("gallery=");
query.append(android.net.Uri.encode(value));
Ellen Spertus
  • 6,576
  • 9
  • 50
  • 101
voghDev
  • 229
  • 2
  • 3
4

I guess you want to replace all spaces, not only white.

the simplest way is to use

"url_with_spaces".replaceAll(" ", "%20");

However, you should consider also other characters in the URL. See What is the recommended way to escape HTML symbols in plain Java?

Kartik Agarwal
  • 1,129
  • 1
  • 8
  • 27
bart
  • 2,378
  • 2
  • 19
  • 21
3

For anyone that needs space characters to be encoded as a %20 value instead of a + value, use:

String encodedString = URLEncoder.encode(originalString,"UTF-8").replaceAll("\\+", "%20")
TheIT
  • 11,919
  • 4
  • 64
  • 56
3
String s = "my string";
s=s.replaceAll(" ", "%20");
djg
  • 1,283
  • 9
  • 6
1

Try using URIUtil.encodePath method from the api org.apache.commons.httpclient.util.URIUtil.

This should do the trick for you.

Tim Kist
  • 1,164
  • 1
  • 14
  • 38
Paul John
  • 11
  • 1