By kswaughs | Thursday, May 14, 2015

How to read content between xml tags

In Java,we have many xml parsers to read the xml string and convert into java objects.
But when we want to read or extract a small portion from xml string then no need to use those parsers.

Simply Use apache commons StringUtils.

String inputXml = "<name>Srinivas</name><city>Hyderabad</city>";
String userName = StringUtils.substringBetween(inputXml, "<name>", "</name>");

The String variable userName will have the value "Srinivas".

To use this class in your applications, You have to download the commons-lang jar of Apache Commons Group.

Download the jar from below link.
https://commons.apache.org/proper/commons-lang/download_lang.cgi

To download the jar from Maven, Use below configuration in your project pom.xml file.

<dependency>
	<groupId>commons-lang</groupId>
	<artifactId>commons-lang</artifactId>
	<version>2.6</version>
</dependency>

Recommend this on


2 comments: