You could use a pattern to convert the format string into a regex and then use this regex to progress the input string, below an example class:
public final class FormatReader {
private final Pattern formatPattern;
private final List<String> names;
private FormatReader(
final Pattern formatPattern,
final List<String> names) {
////
this.formatPattern = formatPattern;
this.names = names;
}
public static FormatReader of(
final String prefix,
final String suffix,
final String format) {
////
return of(prefix, suffix, format, true);
}
public static FormatReader of(
final String prefix,
final String suffix,
final String format,
final boolean allowSurroundingWhitespace) {
////
// This method is somewhat ugly...
final List<String> names = new ArrayList<>();
final StringBuilder sb = new StringBuilder("(?m)");
boolean skip = allowSurroundingWhitespace;
if (skip)
sb.append("\\s*");
for (int i = 0, last = 0, prefixLength = prefix.length(), suffixLength = suffix.length();;) {
if (i == format.length()) {
if (!skip)
sb.append(Pattern.quote(format.substring(last)));
break;
}
if (format.startsWith(prefix, i)) {
skip = true;
sb.append(Pattern.quote(format.substring(last, i))).append("(.+)");
final int off = i + prefixLength;
names.add(format.substring(off, i = format.indexOf(suffix, off)));
i += suffixLength;
continue;
}
if (Character.isWhitespace(format.charAt(i))) {
if (!skip) {
skip = true;
// Replace '\s*' with '\s+' if at least one whitespace has to be present
sb.append(Pattern.quote(format.substring(last, i))).append("\\s*");
}
} else if (skip) {
last = i;
skip = false;
}
i++;
}
if (!skip && allowSurroundingWhitespace)
sb.append("\\s*");
return new FormatReader(Pattern.compile(sb.toString()), names);
}
public Map<String, String> toMap(
final String input) {
////
final Matcher m = formatPattern.matcher(input);
if (!m.matches())
throw new IllegalArgumentException("Argument does not match format");
final Map<String, String> map = new HashMap<>();
for (int i = 0; i < m.groupCount();)
map.put(names.get(i), m.group(++i));
return map;
}
public static void main(
final String[] args) {
////
final FormatReader r = of("${", "}", ""
+ " <data>\n"
+ " <id>${id}</id>\n"
+ " <name>${name}</name>\n"
+ " </data>");
final String s = ""
+ " <data>\n"
+ " <id>900</id> "
+ " <name>Vivek</name>\n"
+ " </data> ";
// The created pattern (accepts any count of whitespace):
// 'id' 'name'
// (?m)\s*\Q<data>\E\s*\Q<id>\E(.+)\Q</id>\E\s*\Q<name>\E(.+)\Q</name>\E\s*\Q</data>\E\s*
System.out.println(r.toMap(s)); // {name=Vivek, id=900}
}
}