Reputation: 684
I have a text file which contains different XML tags. I need to split these tags into key and value pairs. For example the following tag should be changed to Version:1.5
<manifest:Version>1.5</manifest:Version>
Expected output: Version:1.5
Is there any way to do this without using XML Parser?
Upvotes: 0
Views: 1078
Reputation: 479
Since you already mentioned you don't want to use any xml-parser
here is a sample code which will work in you case-
import java.util.Arrays;
import java.util.List;
public class BadXmlParser {
public static void main(String[] args) {
List<String> tags = Arrays.asList("<manifest:Name>java</manifest:Name>", "<manifest:Version>1.8</manifest:Version>");
tags.forEach(tag -> {
String key = tag.substring(tag.indexOf(":") + 1, tag.indexOf(">"));
String value = tag.substring(tag.indexOf(">") + 1, tag.indexOf("</"));
System.out.println(key + ":" + value);
});
}
}
Never recommended approach to use in prod.
Note: You should put validation logic by you own this is just parsing logic provided
Upvotes: 2