Java RegEx: Replace all xml characters with their entity number

1.4k Views Asked by At

I am trying to port a function I wrote in ActionScript to Java and I am having a bit of trouble. I have included the function below. I found this response to question #375420, but do I really need to write a separate class? Thanks.

public static function replaceXML(str:String):String {
  return str.replace(/[\"'&<>]/g, function($0:String):String {
    return StringUtil.substitute('&#{0};', $0.charCodeAt(0));
  });
}

Input

<root><child id="foo">Bar</child></root>

Output

&#60;root&#62;&#60;child id=&#34;foo&#34;&#62;Bar&#60;/child&#62;&#60;/root&#62;

UPDATE

Here is my solution if anyone is wondering. Thanks Sri Harsha Chilakapati.

public static String replaceXML(final String inputStr) {
  String outputStr = inputStr;
  Matcher m = Pattern.compile("[&<>'\"]").matcher(outputStr);
  String found = "";
  while (m.find()) {
    found = m.group();
    outputStr = outputStr.replaceAll(found,
      String.format("&#%d;", (int)found.charAt(0)));
  }
  return outputStr;
}
2

There are 2 best solutions below

2
On BEST ANSWER

You can use regex for that.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

String myString = "<root><child id=\"foo\">Bar</child></root>";

Matcher m = Pattern.compile("[^\\p{L}\\p{N};\"+*/-]").matcher(myString);

while (m.find()) {
    String found = m.group();
    myString = myString.replaceAll(found, "&#" + (int)found.charAt(0) + ";");
}

System.out.println(myString);

It's working.

Output is

&#60;root&#62;&#60;child&#32;id&#61;"foo"&#62;Bar&#60;/child&#62;&60;/root&#62;
1
On

Well Java is an object oriented language, and therefore working with objects. Usually you can create a Util class, e.g. RegExUtil and provide a static method to invoke the method from any other class. The util class itself, shouldn't be instantiated. You can achieve that with a private Constructor.

public class RegExUtil {

  private RegExUtil(){
    //do nth.
  }

  public static String replaceXML(String input){
    //do sth.
  }
}

You should lookup Apache Commons first, because they may already provide a solution for your objective or at least you see how Util classes are made up.