Displaying XML content values on the frontend using the OpenCms Java API

Besides using the OpenCms taglib for displaying XML contents, you can use the Java API provided by the OpenCms CmsJspXmlContentBean class. This class extends the CmsJspActionElement class, which is normally used to provide access to all OpenCms functionalities.

Using the methods contentload and contentloop of this class, you can create objects for accessing the elements of the contents. An example can be found in simplecontentdisplay.jsp in the simpleexample subfolder. This JSP simply displays the contents of simplecontent.html in the same folder without using the OpenCms taglib:

1 <%@ page session="false" import="org.opencms.jsp.*" %>
2 <%
3	CmsJspXmlContentBean thisPage = new CmsJspXmlContentBean(pageContext, request, response);
4	String path = thisPage.getRequestContext().getFolderUri();
8	I_CmsXmlContentContainer contentContainer = thisPage.contentload("singleFile",  
                                                    path.concat("simplecontent.html"), true);
9	I_CmsXmlContentContainer elementContainer;
11	while (contentContainer.hasMoreContent()) {
15		out.write(thisPage.contentshow(contentContainer, "Title"));		
18		elementContainer = thisPage.contentloop(contentContainer, "Teaser");
19		while (elementContainer.hasMoreContent()) {
22			out.write(thisPage.contentshow(elementContainer));	
24		}
30	}
33 %>

The following lines are important to create the output:

  • Line 8: The contentload method initializes the content container with a single file named "simplecontent.html".
    The boolean value set to true indicates that direct edit buttons should be created.
  • Line 11: Using the hasMoreContent method of the container, the contents from the file are loaded into the container. This method must be called before accessing the values!
    It returns true until there is no more piece of content available.
  • Line 15: The method contentshow can be used in order to display a single field value.
  • Line 18/19: If a content may contain more than one value for an element, you can use the contentloop tag to create a new container, and then you can iterate over the available values.