This error happens when you try to parse a returned JSON string in Javascript. Chances are, the string that want to parse as a JSON object is already parsed, hence the error message “Unexpected token o”.

The solution is to use the variable returned to you, say from an asynchronous call as a JSON Object rather than trying to parse it again.

JSoup is a Java library for working with real-world HTML. It provides a very convenient API for extracting and manipulating data, using the best of DOM, CSS and JQuery-like methods.

I had been using the HTMLParser library for so many years even though the library had been stopped way way before because I found it met my needs. But when it came time to parsing a particular site that I wanted to check, I found it hard to parse the HTML code because the web developers did a stupid job of displaying data.

That was when I decided to try JSoup. With very good results!

JSoup implements the WHATWG HTML5 specification and parses HTML to the same DOM as modern browsers do. Other features include:

  • scrape and parse HTML from a URL, file, or string
  • find and extract data, using DOM traversal or CSS selectors
  • manipulate the HTML elements, attributes, and text
  • clean user-submitted content against a safe white-list, to prevent XSS attacks
  • output tidy HTML

JSoup is very easy to use too. Its JQuery like style for searching for HTML portions of the code makes it very convenient and easy to use.

Goodbye old library. JSoup is what I will be using from now on!

Java has a set of classes that will enable you to parse the value of tags in between. These two methods are helper methods that I use in order to get the value of the tag. This is good for first level tags only. If you need to parse xml tags that have depths of more than one level, these methods will not work. This code will only work once you have access to the tag node.

The first method requires you to have reference to the Node object. The String parameter is the tag name itself and it is case sensitive. Changing the equals() to equalsIgnoreCase() will not work either. It is just the way it is. Once you have the node object, you can get the value of the tag by using the second method. You can use both of them right away in one line.

Related Posts Plugin for WordPress, Blogger...