[opencms-dev] contentshow
Paul-Inge Flakstad
flakstad at npolar.no
Fri Aug 28 11:02:19 CEST 2015
Hi Lorenz,
Yes, you can do that with contentshow:
I_CmsXmlContentContainer thisFile = cms.contentload("singleFile", "/the/file/path.html", false);
if (thisFile.hasMoreResources()) {
out.println(cms.contentshow(thisFile, "Teaser[2]", LOCALE));
}
Or alternatively, use the CmsXmlContent class:
CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(cmso, cmso.readFile("/the/file/path.html"));
String elementStringValue = xmlContent.getStringValue(cmso, "Teaser[2]", LOCALE);
Below I've included a complete test JSP - just set the FILE_PATH and LOCALE_STR values.
HTH :)
Cheers,
Paul
<%--
Document : script-get-content-by-xpath.jsp
Created on : Aug 28, 2015
Author : Paul-Inge Flakstad, Norwegian Polar Institute <flakstad at npolar.no>
--%><%@ page import="java.util.List,
java.util.Collections,
java.util.Locale,
java.util.Iterator,
org.opencms.jsp.I_CmsXmlContentContainer,
org.opencms.jsp.CmsJspActionElement,
org.opencms.jsp.CmsJspXmlContentBean,
org.opencms.file.CmsObject,
org.opencms.file.CmsResource,
org.opencms.file.CmsFile,
org.opencms.xml.content.CmsXmlContent,
org.opencms.xml.content.CmsXmlContentFactory"
%><%
// Action element and CmsObject
CmsJspXmlContentBean cms = new CmsJspXmlContentBean(pageContext, request, response); // For the CmsXmlContent approach, you can use CmsJspActionElement instead
CmsObject cmso = cms.getCmsObject();
final String ELEMENT_PATH = "SomeElement[2]/ChildElement[1]";
final String FILE_PATH = "/the/file/path.html";
final String LOCALE_STR = "en";
final Locale LOCALE = new Locale(LOCALE_STR); // See also CmsXmlContent#getLocales()
// CmsJspXmlContentBean#contentshow() approach
try {
I_CmsXmlContentContainer thisFile = cms.contentload("singleFile", FILE_PATH, false);
if (thisFile.hasMoreResources()) {
out.println(cms.contentshow(thisFile, ELEMENT_PATH, LOCALE));
}
} catch (Exception e) {
out.println("<pre>");
e.printStackTrace(new java.io.PrintWriter(out));
out.println("</pre>");
}
// CmsXmlContent approach (more comprehensive example)
try {
// Read the file and build the xml content instance
CmsFile xmlContentFile = cmso.readFile(FILE_PATH);
CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(cmso, xmlContentFile);
// Get all the element names that are available in the file (for that locale)
List<String> elementNames = xmlContent.getNames(LOCALE);
Collections.sort(elementNames);
Iterator<String> iElementNames = elementNames.iterator();
if (iElementNames.hasNext()) {
%>
<h1>File content by element – <%= FILE_PATH %></h1>
<%
while (iElementNames.hasNext()) {
String elementName = iElementNames.next();
%>
<div class="element" style="margin:0; padding:1em 0; border-bottom:3px solid orange;">
<h2 style="font-family:monospace; display:inline-block; background:lightyellow; padding:1em; margin:0;"><%= elementName %></h2>
<%
try {
// If the element is a wrapper for nested elements, trying to get the string value will throw an exception
String elementStringValue = xmlContent.getStringValue(cmso, elementName, LOCALE);
%>
<div style="padding:1em; background:#eee;"><%= elementStringValue %></div>
<%
} catch (Exception e) {
%>
<span style="color:#999;">Skipped. <em><%= e.getMessage() %></em></span>
<%
}
%>
</div>
<%
}
}
} catch (Exception e) {
out.println("<pre>");
e.printStackTrace(new java.io.PrintWriter(out));
out.println("</pre>");
}
%>
-----Original Message-----
From: opencms-dev-bounces at opencms.org [mailto:opencms-dev-bounces at opencms.org] On Behalf Of Lorenz Lammersdorf
Sent: 26. august 2015 11:24
To: The OpenCms mailing list
Subject: [opencms-dev] contentshow
guys,
the contenshow tag allows to address a specific element directly, like
this: <cms:contentshow element="Teaser" index="2" />
is this possible in java-notation too, like thisPage.contentshow(contentContainer, "Teaser[2]")?
thanks
lorenz
_______________________________________________
This mail is sent to you from the opencms-dev mailing list To change your list options, or to unsubscribe from the list, please visit http://lists.opencms.org/cgi-bin/mailman/listinfo/opencms-dev
More information about the opencms-dev
mailing list