Modify file content programatically

From OpenCms Wiki
Revision as of 11:55, 24 July 2012 by Paul (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This is a short example on how to modify an XML content (structured content) file programatically. (The code is based on a blog post by Sebastian Himberger.)

<%@page import="java.util.Locale" %>
<%@page import="org.opencms.jsp.CmsJspActionElement" %>
<%@page import="org.opencms.file.*" %>
<%@page import="org.opencms.xml.types.*" %>
<%@page import="org.opencms.xml.*" %>
<%@page import="org.opencms.xml.content.*" %>
<%
CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
CmsObject cmso = cms.getCmsObject();
 
// Locale should be set to the same locale that you want to manipulate for
Locale locale = cms.getRequestContext().getLocale(); 
 
// Log in special user and make sure project is set to "Offline"
cmso.loginUser("my_script_user", "password");
if (cms.getRequestContext().currentProject().isOnlineProject()) {
	cms.getRequestContext().setCurrentProject(cmso.readProject("Offline"));
}
 
// Read the XML/structured content resource to modify
String resourcePath = "/path/to/the-file-to-modify.html";
CmsResource resource = cmso.readResource(resourcePath); // Also possible to use the createResource method here
cmso.lockResource(resourcePath);
CmsFile file = cmso.readFile(resource);
CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(cmso, file); 
 
/////////////////////////
// Manipulate the content
//
 
// Non-optional <element name="Title" type="OpenCmsString" />
xmlContent.getValue("Title", locale).setStringValue(cmso, "My new title");
// Non-optional <element name="Description" type="OpenCmsHtml" />
xmlContent.getValue("Description", locale).setStringValue(cmso, "<p>My <em>new</em> description!</p>");
 
// Non-optional nested element ("Type"), containing 2 OpenCmsBoolean elements ("Foo" and "Bar")
I_CmsXmlContentValue typeValue = xmlContent.getValue("Type", locale);
xmlContent.getValue(typeValue.getPath() + "/Foo", locale).setStringValue(cmso, "true");
xmlContent.getValue(typeValue.getPath() + "/Bar", locale).setStringValue(cmso, "false");
 
// Optional <element name="Link" type="OpenCmsVarLink" minOccurs="0" maxOccurs="100" />
// Note the different method call for optional elements; they need to be added to the XML
// (If order is important, use a counter instead of just zero for the 4th "insert at index" argument)
xmlContent.addValue(cmso, "Link", locale, 0).setStringValue(cmso, "http://first-site.com"); 
xmlContent.addValue(cmso, "Link", locale, 0).setStringValue(cmso, "http://second-site.com"); 
xmlContent.addValue(cmso, "Link", locale, 0).setStringValue(cmso, "http://third-site.com");
 
//
// Done manipulating
////////////////////
 
// Replace the original file content with the manipulated file content ("commit changes")
file.setContents(xmlContent.marshal());
cmso.writeFile(file);
 
// Unlock the resource
cmso.unlockResource(resourcePath); 
%>
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox