Parsing in Liferay(Using SAX Parser reader)
Liferay provided default classes to parse the xml content.
Have look at this SAXReaderUtil which will be useful to parse the xml.
For Example:
Consider the WebContent(Journal article) where we have added metadata ,consider to fetch it.
First of all this metadata content will be store in Journal article table of Content column as
<?xml version="1.0"?>
<root available-locales="en_US" default-locale="en_US">
<dynamic-element instance-id="trtrtrt454" name="Title" type="text" index-type="">
<dynamic-content><![CDATA[Test Title]]></dynamic-content>
</dynamic-element>
<dynamic-element instance-id="Adgbcd456" name="Desc" type="text" index-type="">
<dynamic-content><![CDATA[Test Desc]]></dynamic-content>
</dynamic-element>
</root>
We have fetch the meatdata field Title of value "Test Title"
Here we go,
String xmlContent = journalArticle.getContent();
String title = "Title";
Document document = SAXReaderUtil.read(xmlContent);
Node node = document .selectSingleNode("/root/dynamic-element[@name='" + title + "']/dynamic-content");
String value = node.getText();
System.out.println("value is"+value);
Output is: value is : Test Title
Thats it here we got value of the metadata value.
Thanks, exactly what i was looking for!
ReplyDeletehow to get repeatable fields using this....suppose i have one field name="title" and inside this repeatable field one field name="description" than for repeatable field i can get using List node = document.selectnodes(""); but for inner field how to get can u please rply......
ReplyDeleteSometimes for other XML's, it does not show the output.
ReplyDeleteI found this solution to be working for me in that scenarios:
For the same above XML file content and to get the output value as "Test Title", i used below snippet for Node syntax:
Node node = document.selectSingleNode("/root/dynamic-element[@name='Title']/dynamic-content");
Other code is same as above.
hi can u please suggest how to change "TEST TITLE" value to "HELLO WELCOME" & get changed Full XML Content AS .
ReplyDeleteHow to parse Nested Elements ??
ReplyDelete