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,101
  • 7
  • 54
  • 72
SRam
  • 2,794
  • 4
  • 43
  • 71

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,140
  • 7
  • 38
  • 54
Sumant
  • 2,775
  • 2
  • 21
  • 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
  • +1 Thank you for solution. It worked like a charm! You saved my day man ;) Thank you so much again ;) – Simon Dorociak Sep 01 '14 at 20:25
  • 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.

sudocode
  • 15,997
  • 40
  • 59
  • 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
  • 7
    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. – sudocode Dec 10 '18 at 16:53
21

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,328
  • 9
  • 51
  • 90
voghDev
  • 219
  • 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 Recommended method for escaping HTML in Java

Community
  • 1
  • 1
bart
  • 2,358
  • 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,413
  • 3
  • 62
  • 54
3
String s = "my string";
s=s.replaceAll(" ", "%20");
djg
  • 1,273
  • 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,138
  • 1
  • 14
  • 38
Paul John
  • 11
  • 1