Thursday, July 28, 2016

Get value from Structure Field for Journal Article/Web Content in Liferay.


During your liferay development, many time you need get value from DDM Structure field. So, here i have piece of code which gives you value of structure field for journal article/ web content.

Suppose you have article content field data(from JournalArticle table) something like (Mention only one field for example.)...


<?xml version="1.0"?>

 <root available-locales="en_US" default-locale="en_US">
  <dynamic-element name="country" type="text" index-type="" index="0" instance-id="Page_Title">
  <dynamic-content language-id="en_US"><![CDATA[India]]></dynamic-content>
  </dynamic-element> 
 </root>


Then your code to get value of country field is like...

JournalArticle article = JournalArticleLocalServiceUtil.getArticle(...);

Document document = SAXReaderUtil.read(article.getContent());

Node node = document.selectSingleNode("/root/dynamic-element[@name='country']/dynamic-content");

String country = node.getText();


if you have multiple field then your code looks like...(Suppose your field have two times as repeated field )

1) Access first element value is....

Node node = document.selectSingleNode("/root/dynamic-element[@name='country' and @index='0']/dynamic-content");
String country = node.getText()


2) Access second element value is....

Node node = document.selectSingleNode("/root/dynamic-element[@name='country' and @index='1']/dynamic-content");
String country = node.getText()



HTH...!!!


Thanks,
Ketan Savaliya





No comments:

Post a Comment