I need one help regarding my xml . I want to remove space between the element name of xml tags . for eg : I have one xmlstring it consists of xml tags like <Customer name><address book > like this so i want to remove space between customer & name . it should look like <customername>&<addressbook>. Only spaces between xml tags element name. Please help on this using xslt or java.
1 Answer
If we match on patterns that contain white space in between two brackets < and > then we can take those groups and do a replace all of the white space and re-insert them into the original string.
String input = "<Customer name> <address book >";
Pattern p = Pattern.compile("(<[^>]*?\\s[^>]*?>)");
Matcher m = p.matcher(input);
StringBuffer result = new StringBuffer();
while (m.find()) {
String replace = m.group().replaceAll("\\s+", "");
m.appendReplacement(result, replace);
}
m.appendTail(result);
System.out.println(result);
This would print out . The pattern looks for a starting brace, matches on anything that isn't an ending brace until it finds white space, then matches on anything not an ending brace until it finds an ending brace. If we find a match, we remove the white space and replace the match back into the input string.
<Customer name>, since that is not valid XML. And because of that, you cannot read it using an XML parser, so you cannot fix it.