Pages

Saturday 21 April 2012

Fetching Data from the JournalArticle Cotent ie. XML File using SAXReaderUtil

Fetching Data  from the JournalArticle Cotent ie. XML File using SAXReaderUtil
Hi everyone ,
Here is a small code snippet for fetching required content(field) from the web content article.

Before proceeding further I m assuming you know how to create a structure in Webcontent Portlet.

As we all know that whenever we create a structure and create a WebContent article ,it is stored in JouranlArticle Table under ‘content’ in the form an XML file.

So fetching this content in the view part is easy i.e. we just need to call String contentXml      = journalArticle.getContent();
which returns an xml file in the form of a String. But now fetching required fields out of this XML file is user choice.

There are many ways to do this, but here I will show how to use Liferay API classes to read this XML file and fetch required Field-Values.

In Below code I have written some String Constants in Capitals.You can create a separate Constant Class and declare these values there.For convience I have created them in my SaxUtilClass .

SaxUtilClass has a static method defined “getNodeValue” which takes 2 parameters.
1.  The ContentXml String
2.  The Field Name which you have defined in your Structure

import com.liferay.portal.kernel.xml.Document;
import com.liferay.portal.kernel.xml.DocumentException;
import com.liferay.portal.kernel.xml.Node;
import com.liferay.portal.kernel.xml.SAXReaderUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.Validator;

public abstract class SaxUtilClass {
static String ARTICLE_CONTENT_XML_NODE_START= "/root/dynamic-element[@name='";
static String ARTICLE_CONTENT_XML_NODE_END= "']/dynamic-content";
static String ARTICLE_STATUS= "STATUS";
static String ARTICLE_ACTIVE= "Active"
           
      public static String getNodeValue(String contentXml, String nodeName)
        throws DocumentException, PortalException, SystemException
      {
            String value = "";
            Document doc = SAXReaderUtil.read(contentXml);       
            Node node = doc.selectSingleNode(ARTICLE_CONTENT_XML_NODE_START +
                                                      nodeName +                                                  ARTICLE_CONTENT_XML_NODE_END );
           
if(Validator.isNotNull(node) && node.getText().length() > 0){
                  value = node.getText();
            }
                       
            return value;
      }
     
     
Now in Business class fetching  JournalArticle Content and value for specific Field.
String content    = "";
JournalArticle    journalArticle    = JournalArticleLocalServiceUtil.getArticle(id);
try{
content = SaxUtilClass.getNodeValue(journalArticle.getContent(), "IMAGE");      }catch (DocumentException de){
_log.warn("There is an Exception while pulling IMAGE Value from the Content XML file",de);
}
     

Hope this is useful .
Regards
Srikanth S

1 comment:

  1. Hi Srikanth,
    My requirement in liferay is to dynamically populate the drop down list in jsp, the data for this is in a xml file. I do not know how to do it. Please help

    ReplyDelete