27 Sep 2005

Clean and neat

Yes sometimes Java can be neat and clean ;)
I was trying to extract a node value from an xml stored in a String, I made this little piece of code (and I like it). No tiedous try/catch handling, simple and clear (thanks JXPath):

public String extractNumberFromResponse(String responseBody) {
org.apache.commons.jxpath.xml.DOMParser parser = new DOMParser();
Object xml = parser.parseXML(new java.io.StringBufferInputStream(responseBody));
org.apache.commons.jxpath.JXPathContext context = JXPathContext.newContext(xml);

return (String)context.getValue("/my:jlogin/my:session");

No comments: