How To Convert Java Regex To Extract Text Between Tags?

2.03K viewsJava

I have an XML file with some tags and I’d like to take exact tags data to form XML string between the tags.
For example, I need to take the name tag value.


Changed status to publish
Add a Comment

Use below code .

private static final Pattern TAG_REGEX = Pattern.compile("<Name>(.+?)</Name>", Pattern.DOTALL);
	public static void main(String[] args) throws IOException {
		String file="<GetResponse>\r\n" + 
				"			<RuleInfo>\r\n" + 
				"				<Name>clarifyall</Name>\r\n" + 
				"				<DataType>RichTextFormat</DataType>\r\n" + 
				"			</RuleInfo>\r\n" + 
				"	</GetResponse>";

	private static List<String> getTagValues(final String str) {
	    final List<String> tagValues = new ArrayList<String>();
	    final Matcher matcher = TAG_REGEX.matcher(str);
	    while (matcher.find()) {
	    return tagValues;





Changed status to publish
Add a Comment
You are viewing 1 out of 1 answers, click here to view all answers.
Write your answer.