0

I am trying to parse below string in java servlet.

<con>
   <status>OK</status>
   <session>12312332432</session>
 </con>

I want value of <session> element.

Ideas ?

7
  • That is an entire String ? Commented Aug 1, 2013 at 11:01
  • Looks an awful lot like XML this.. Commented Aug 1, 2013 at 11:02
  • original string is very big. this is just sample Commented Aug 1, 2013 at 11:02
  • @Deckard27 getElementByTagName?? Commented Aug 1, 2013 at 11:03
  • @Deckard27 - You thinking JS not Java? Commented Aug 1, 2013 at 11:06

3 Answers 3

3

If it's not XML, you can use regex:

import java.util.regex.*;

String aParser="<con><status>OK</status><session>12312332432</session></con>";
Pattern p=Pattern.compile("<session>(.*)</session>");
Matcher m=p.matcher(aParser);

while(m.find())
{ 
   System.out.println(m.group(1));
}
Sign up to request clarification or add additional context in comments.

Comments

1

For XML purposes you should use an XMLParser.

Look at SAXParser it will do the job.

SAXParser is very good when dealing with big files, because the whole document isn´t hold in memory.

2 Comments

its not XML. its just html string
The String you posted can be parsed with an XML Parser.
0

If you just need session value and not the entire XMl data.just split() the string as below

xmlString.split(starttag)[1].split(endtag)[0];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.