RE: How to get Web Content/Journal Article Fields Data Programmatically in 7.4

Jamie Sammons, modified 2 Years ago. Junior Member Posts: 27 Join Date: 8/31/14 Recent Posts

Hi All,

I have an web content which contains different type of fields in structure like text,rich text, radio etc, after creating  webcontent 
i am trying to read the data programatically below is the code used to get the content in xml

JournalArticle journalObj = JournalArticleLocalServiceUtil.getLatestArticle(resourcePrimKey,
                        WorkflowConstants.STATUS_APPROVED);
logger.info(journalObj.getContentByLocale(locale.toString()));

 

Whenever select or radio button option is selected in structure , instead of actual value some reference code is getting stored in xml content.
Below is the xml content, in older version of liferay there was content field in the article table itself which has been removed in 7.4.


What is the new way to extract article field data programatically in Lifeary 7.4?

Out put:
<?xml version="1.0"?>__
<root available-locales="en_US" default-locale="en_US" version="1.0">_  
    <dynamic-element field-reference="Category" index-type="keyword" instance-id="idIMg6lU" name="Text77899654" type="text">_    
        <dynamic-content language-id="en_US">
            <![CDATA[category value]]>
        </dynamic-content>_  
    </dynamic-element>_  
    <dynamic-element field-reference="DefaultSearchResult" index-type="keyword" instance-id="8wLqQTPg" name="SingleSelection68890196" type="radio">_    
        <dynamic-content language-id="en_US">
            <![CDATA[Option03769730]]>
        </dynamic-content>_  
    </dynamic-element>_
</root> [Sanitized]

Liferay version used: Liferay Digital Experience Platform 7.4.13 Update 40

thumbnail
Jamie Sammons, modified 2 Years ago. Liferay Master Posts: 593 Join Date: 8/8/14 Recent Posts

Hi ,

You can use JournalConverter for extracting fields from Webcontent. 
 

Fields fields =  journalConverter.getDDMFields(journalArticle.getDDMStructure(), journalArticle.getContent());

fields.get("fieldName").getValue();

 

Jamie Sammons, modified 2 Years ago. Junior Member Posts: 27 Join Date: 8/31/14 Recent Posts

Hi Yasin,

If you see the below generated xml content name attribute is autogenerated where as field-reference attribute is taken from the field reference attribute of the webcontent structure

 <dynamic-element field-reference="DefaultSearchResult" index-type="keyword" instance-id="8wLqQTPg" name="SingleSelection68890196" type="radio">_    
        <dynamic-content language-id="en_US">
            <![CDATA[Option03769730]]>
        </dynamic-content>_  
    </dynamic-element>_

so the snippet provided by you need name attribute which is autogenerated in this case, i have found a work around by using below snippet able to get the actual value of the field.Hope this helps to others !!

JournalArticle journalObj = JournalArticleLocalServiceUtil.getLatestArticle(resourcePrimKey,
						WorkflowConstants.STATUS_APPROVED);
		Document document = SAXReaderUtil.read(journalObj.getContentByLocale(locale.toString()));
		List<DDMFormField> ddmFormFields = journalObj.getDDMStructure().getDDMForm().getDDMFormFields();
		String content = "";
		Node fieldNode = document.selectSingleNode("/root/dynamic-element[@field-reference='" + "FieldReferenceName" + "']/dynamic-content");
content =fieldNode != null ? fieldNode.getText() : ""
		DDMFormField ddmFormField = ddmFormFields.stream()
					.filter(ref -> nodeName.equalsIgnoreCase(ref.getFieldReference())).findAny().orElse(null);
			if (ddmFormField != null) {
				content = ddmFormField.getDDMFormFieldOptions().getOptionReference(content);
			}