### External Jar JSoup

Using JSoup with help of Library JavaObject.
Additional jar to include: jsoup-1.8.1
Reference: http://www.jsoup.org
Notes:
• The version of the additional Jar might have changed. Lookup the related website.
Project Attributes
#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 400
'External lib
#AdditionalJar: jsoup-1.8.1
#End Region
Example Connect & Select HTML data
'From the jsoup homepage example:
'Fetch the Wikipedia homepage, parse to a DOM, and select the headlines from In the news section into a list of Elements
'Reference: http://www.jsoup.org
`Sub jSoup_Connect_Select
Log("Jsoup Connect Select")
Dim joDoc As JavaObject
'Log("New Instance...")
joDoc.InitializeStatic("org.jsoup.Jsoup")
'Java:Document doc = Jsoup.connect("http://en.wikipedia.org/").get();
'Log("Connect...")
joDoc = joDoc.RunMethod("connect", Array("http://en.wikipedia.org/"))
'Log("Connected...")
Dim joDocGet As JavaObject
joDocGet =joDoc.RunMethod("get", Null)
'Log("Get done...")
Dim l As List
l.Initialize
'Java:Elements newsHeadlines = doc.Select("#mp-itn b a");
l = joDocGet.RunMethod("select", Array("#mp-itn b a"))
Log("Headlines found: " & l.Size)
For i = 0 To l.Size - 1
Log(l.Get(i))
Next
End Sub`
Example parsing HTML & traversing a Document
'From the jsoup example:
'Parsing & traversing a Document
'Reference: http://jsoup.org/cookbook/introduction/parsing-a-document
`Sub jSoup_Parse_HTML
Log("jsoup Parse HTML")
Dim shtml As String = "
" & _
"

Parsed HTML into a doc.

"
'Log("New Instance...")
Dim joDoc As JavaObject
joDoc.InitializeStatic("org.jsoup.Jsoup")
Dim s As String = joDoc.RunMethod("parse", Array(shtml))
Log(s)
End Sub`
Example Parse HTML from Connect & Select HTML Data
`Sub jSoup_Connect_Select As String
Dim result As String = ""
Dim joDoc As JavaObject
joDoc.InitializeStatic("org.jsoup.Jsoup")
joDoc = joDoc.RunMethod("connect", Array("http://en.wikipedia.org/"))
Dim joDocGet As JavaObject
joDocGet =joDoc.RunMethod("get", Null)
Dim l As List
l.Initialize
l = joDocGet.RunMethod("select", Array("#mp-itn b a"))
Dim sb As StringBuilder
sb.Initialize
For i = 0 To l.Size - 1
sb.Append(l.Get(i) & CRLF)
Next
result = sb.ToString
Return result
End Sub`
`Sub jSoup_Parse_HTML2 As String
Dim result As String = ""
Dim joDoc As JavaObject
joDoc.InitializeStatic("org.jsoup.Jsoup")
result = joDoc.RunMethod("parse", Array(jSoup_Connect_Select))
Log(result)
Return result
End Sub`
OR optimized
`Sub jSoup_Parse_HTML2 As String
Dim joDoc As JavaObject
Return joDoc.InitializeStatic("org.jsoup.Jsoup").RunMethod("parse", Array(jSoup_Connect_Select))
End Sub`

Changed: , Category: Jar, File: T6C204M1499.htm, ID: 1499