Powerful – Sling Model and Sightly with JSOUP in AEM 6.x

 

AEM 6.x introduced Sightly which very powerful template system and Sling Model libraries are already present in OSGI. For AEM 5.6.1 users, see this article which shows how you can use sightly in 5.6.1 and Add Sling Model dependency in your pom.xml. Put a comment below if you are not aware.

JSOUP is one of very useful DOM parser library available, It provides a very convenient API for extracting and manipulating data, using the best of DOM, CSS, and jquery-like methods.

In real world, there will a lots of time you will asked by your cool manager to parse some webpage’s DOM (Document Object Model). It may to extract images, links, content or you might be asked to develop some content migration tool.

Our component in AEM will accept a URL. Based on the given URL what you want to extract you can check using available checkboxes.

Example: I have the URL as ” http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manager.html “. If I want to extract Images from this URL, I should check Images.

Sightly DOM Parser
Sightly DOM Parser

 

Here are the steps to get this awesome stuff done:

  1. Create a component, which should have
    • Textarea for URL, URLs are pretty long to its better to get textarea than textfield
    • Three checkboxes, each for Images, Hyperlinks, Imports ( css files in imported in webpage )
  2. As we have sightly here in .html file of component
  3. company.myproject.components is my Sling Model class which is backed by com.mycompany.myproject.components.Parser a JSOUP Parser Class
  4. Sling Model
  5. Parser

    com.mycompany.myproject.components.Parser:- This class have 3 function each for extracting Hyperlink, Image, Imports which is returned to Sling Model class which later return this to Sightly file in Component.

Try to get this working on your AEM Instance, We have shared the complete article on Adobe AEM Community

4 thoughts on “Powerful – Sling Model and Sightly with JSOUP in AEM 6.x

  1. Hi,
    Thanks a lot for the article and the contribution on “Adobe CQ Help”.
    Could you please explain the purpose of using the GeocodeProvider interface here?
    Thanks,
    G

     
    1. Hi Gaetan,
      Glad you liked it. It’s actually the part of some other module. I hope it doesn’t cause you much trouble

       
  2. Jsoup.connect(url).get();

    is throwing me a null point error.

    some dependency issue.

    can you help me with this !

     

Leave a Reply

Your email address will not be published. Required fields are marked *