Parsing a string in java

I have a string of the form <user>id</user>text<user>id2</user>text1<user>id3</user>
I want to convert this String to <user>name</user>text<user>name2</user>text1<user>name3</user>
Basically each userId has a corresponding name which i need to fetch from DB and replace this id with the name.
How do i do it effeciently in java.

Rohit BajajAsked:
Who is Participating?
Pawan KumarConnect With a Mentor Database ExpertCommented:
Please try with REGEX.
ste5anConnect With a Mentor Senior DeveloperCommented:
What about using XML and XSLT?
Your problem has nothing to do with "parsing a string". It's to do with filling a template. You need to replace placeholders (id,id2,id3 etc) with the actual values. Commonly, this is often done with templating software such as Velocity
The same could go for
Rohit BajajAuthor Commented:
There is one reason why templating probably could not apply here... correct me if i am wrong...
In the above string i need to extract the id and then search for a corresponding name in DB and replace it there
So i need to fetch the id , id2 etc. values which will require parsing it..
Oh sorry - i thought it was the other way around ;)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.