Loading Java Libraries on the Fly (and Parsing RSS Easily)
See, for fun I was trying to load a java library with methods for xml parsing that includes methods for parsing RSS (yes, I know I need a life). You can load this jar using Spike's method, but only an empty instance. To actually load in an RSS feed, you need to supply a URL to the constructor, however there was no way to access the constructor (I tried several methods to no avail). Mark's method solves this issue. Here's an example.First of all, I downloaded the javaLoader.cfc that Mark recently committed within util folder of the Transfer ORM Library. Mark's component loads all the jar files in Transfer's lib directory. I tweaked it a little to add a getter and setter for allowing you to customize the directory when you initialize the component (this also required moving a few lines in the init as well), but it was a simple change.
Now, all I need to do is load the component and point it to where my mjxml.jar file is kept (for the purposes of this example, everything is in the same directory) and Shazam! I have an easy-to-use RSS parser. Well, there is a little more to it than that, so here is the code:
<cfset javaLoader = createObject("component","javaLoader").init(".") />
<cfset rssURL=createObject('java','java.net.URL')>
<cfset rssURL.init("http://www.remotesynthesis.com/blog/rss.cfm?mode=short") />
<cfset rss = javaLoader.create("com.myjavatools.xml.Rss").init(rssURL)>
<cfset arrItems = rss.getItems() />
<cfloop from="1" to="#arrayLen(arrItems)#" index="i">
<cfoutput>
<p>
<strong>
<a href="#arrItems[i].getLink()#">#arrItems[i].getTitle()#</a>
</strong>
<br /> #arrItems[i].getDescription()# </p>
</cfoutput>
</cfloop>
That's it! You can see an example here. As you can see, that little bit of code gave me access to quickly and easily parsing my RSS feed just by supplying the URL on initialization. Anyway, I don't know how useful this mjxml library will be in the end (like I said, it was just messing around and I am new to Java), but I intend to explore it further. However, I do know that Mark's class loader opens up a load of possibilities.
Glad to see you enjoyed using the code!

