Fórum

Read JournalArticle content

thumbnail
Althaf Hussain, modificado 10 Anos atrás.

Read JournalArticle content

Junior Member Postagens: 60 Data de Entrada: 03/05/12 Postagens Recentes
Hi All,

How to read all the "content [is a column in journal article table] "data from JournalArticle table. I want all the content in a single xml file . Can any body provide me a piece of code for that.

URGENT..............

Regards,
AlthafHussain.
thumbnail
Tonu Sri, modificado 10 Anos atrás.

RE: Read JournalArticle content

Regular Member Postagens: 197 Data de Entrada: 15/04/11 Postagens Recentes
Hi Altaf,

You can refer JournalArticleLocalServiceUtil class to access JournalArticle Table.

For example:

JournalArticle myarticle = JournalArticleLocalServiceUtil.getArticle(),

This method is available with different different arguments. For example to get latest content on behalf of version etc.

You can use as per your requirement. Once you get the article, You can extract data from that.

String mycontent = myarticle.getContent();

This method will provide content in XML format, you can parse it through SAXReaderUtil.

Thanks:
Tonu
thumbnail
Althaf Hussain, modificado 10 Anos atrás.

RE: Read JournalArticle content

Junior Member Postagens: 60 Data de Entrada: 03/05/12 Postagens Recentes
Hi Tony,

Plz find the below code:

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;
}
}



import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.xml.DocumentException;
import com.liferay.portlet.journal.model.JournalArticle;
import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

public class XmlData extends MVCPortlet {


public void doView(javax.portlet.RenderRequest renderRequest, javax.portlet.RenderResponse renderResponse) throws java.io.IOException ,javax.portlet.PortletException {};
{

String content = "";
/*long id = 0;
JournalArticle journalArticle = null;*/
try {
JournalArticle journalArticle = JournalArticleLocalServiceUtil.getArticle(id);
System.out.println("id===="+journalArticle);
} catch (PortalException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SystemException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
content = SaxUtilClass.getNodeValue(journalArticle.getContent(), "IMAGE");
System.out.println("content==="+content);
}catch (DocumentException de){

//_log.warn("There is an Exception while pulling IMAGE Value from the Content XML file",de);
} catch (PortalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
it is showing error in the below two lines



JournalArticle journalArticle = JournalArticleLocalServiceUtil.getArticle(id);
content = SaxUtilClass.getNodeValue(journalArticle.getContent(), "IMAGE");


I have already said , i want to read the all data of content column in single xml file.

Regards,
AlthafHussain