We already see in my previous blog on Structues and Tempelates that Liferay Stores Structues in xml format. So if you create a Web Content using Structures and Template and want to get the value of fields programmatically , than you can use SAXReaderUtil Class of Liferay. This class is used to parse xml content.
Ex- I already Created a Structure MOBILE_STRUCTURE which contain 5 fields Brand,Model,Description,Camera and Price.Now i create web contents Sony Xperia C using this structures .This data is save in journalarticle table as:-
Here the Content column store the data in xml as:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<root available-locales="en_US" default-locale="en_US"> | |
<dynamic-element name="Brand" index="0" type="text" index-type="keyword"> | |
<dynamic-content language-id="en_US"><![CDATA[Sony]]></dynamic-content> | |
</dynamic-element> | |
<dynamic-element name="Model_Number" index="0" type="text" index-type="keyword"> | |
<dynamic-content language-id="en_US"><![CDATA[Experia C]]></dynamic-content> | |
</dynamic-element> | |
<dynamic-element name="Description" index="0" type="text" index-type="keyword"> | |
<dynamic-content language-id="en_US"><![CDATA[Contain Front Camera]]></dynamic-content> | |
</dynamic-element> | |
<dynamic-element name="Camera" index="0" type="list" index-type="keyword"> | |
<dynamic-content language-id="en_US"><![CDATA[Yes]]></dynamic-content> | |
</dynamic-element> | |
<dynamic-element name="Price" index="0" type="text" index-type="keyword"> | |
<dynamic-content language-id="en_US"><![CDATA[12000]]></dynamic-content> | |
</dynamic-element> | |
</root> |
So What if we want to fetch the fields like Price of Sony Xperia C programmatically. Just use SAXReaderUtil as:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Document document = SAXReaderUtil.read(journalArticleObject.getContentByLocale(Locale.ENGLISH.toString())); | |
Node brandNode = document.selectSingleNode("/root/dynamic-element[@name='Brand']/dynamic-content"); | |
String brand = brandNode.getText(); | |
Node modelNumberNode = document.selectSingleNode("/root/dynamic-element[@name='Model_Number']/dynamic-content"); | |
String modelNumber = modelNumberNode.getText(); | |
Node descriptionNode = document.selectSingleNode("/root/dynamic-element[@name='Description']/dynamic-content"); | |
String description = descriptionNode.getText(); | |
Node cameraNode = document.selectSingleNode("/root/dynamic-element[@name='Camera']/dynamic-content"); | |
String camera = cameraNode.getText(); | |
Node priceNode = document.selectSingleNode("/root/dynamic-element[@name='Price']/dynamic-content"); | |
String price = priceNode.getText(); |
For getting JournalArticle object you may refer my previous blog on Fetch Web Content Programmatically.
Hope this will Help....
Related Post:-