Real'sHowTo AddThis Feed Button
Custom Search

Strip extra spaces in a XML stringTag(s): XML


This can be useful to reduce the size of a file or before writting a message to a queue.
public class StripSpaces {
  public static void main (String [] args) {
      String test1 = "<tag>test 1</tag>    <tag>test 2</tag> ";
      String out1 = test1.replaceAll(">\\s+<", "><");
      System.out.println(test1);
      System.out.println(out1);

      System.out.println("");

      String test2 = "<tag>test 3</tag> \n<tag>test 4</tag> ";
      String out2 = test2.replaceAll(">\\s+<", "><");
      System.out.println(test2);
      System.out.println(out2);

      /*
      output :

      <tag>test 1</tag>    <tag>test 2</tag>
      <tag>test 1</tag><tag>test 2</tag>

      <tag>test 3</tag>
      <tag>test 4</tag>
      <tag>test 3</tag><tag>test 4</tag>
      */
  }
}

blog comments powered by Disqus


If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2013
[ home ]