<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Thoughts Electrique &#187; Example</title>
	<atom:link href="http://www.sebastian.himberger.de/blog/tag/example/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sebastian.himberger.de/blog</link>
	<description>Sebastian Himbergers blog about technology and software development</description>
	<lastBuildDate>Sat, 16 Jul 2011 22:48:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Manipulating OpenCms XMLContent programatically (some examples)</title>
		<link>http://www.sebastian.himberger.de/blog/2008/12/12/manipulating-opencms-xmlcontent-programatically-some-examples/</link>
		<comments>http://www.sebastian.himberger.de/blog/2008/12/12/manipulating-opencms-xmlcontent-programatically-some-examples/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 15:12:15 +0000</pubDate>
		<dc:creator>Sebastian</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[OpenCms]]></category>
		<category><![CDATA[Sourcecode]]></category>

		<guid isPermaLink="false">http://www.sebastian.himberger.de/blog/?p=253</guid>
		<description><![CDATA[Because someone asked on the mailing list and I keep forgetting the respective API calls to manipulate an OpenCms XMLContent programatically. This is just a raw code dump not a step by step tutorial. You can also download a ZIP file for importing it into OpenCms. But you have to adjust the paths manually. Example [...]]]></description>
			<content:encoded><![CDATA[<p>Because someone asked on the mailing list and I keep forgetting the respective API calls to manipulate an OpenCms XMLContent programatically.</p>
<p><strong>This is just a raw code dump not a step by step tutorial</strong>.</p>
<p><a href="http://www.sebastian.himberger.de/blog/wp-content/uploads/2008/12/example-xmlcontent-20081212.zip">You can also download a ZIP file</a> for importing it into OpenCms. But you have to adjust the paths manually.<a href="http://www.sebastian.himberger.de/blog/wp-content/uploads/2008/12/example-xmlcontent-20081212.zip"><br />
</a></p>
<p><span id="more-253"></span></p>
<h3>Example JSP calls</h3>
<pre class="file">&lt;%@ page import="org.opencms.file.*,
                 org.opencms.jsp.*,
                 org.opencms.xml.*,
                 org.opencms.xml.content.*,
                 org.opencms.xml.types.*,
                 org.opencms.util.*,
                 java.util.*" %&gt;&lt;%

// The resource to read
String resource = "/system/modules/de.himberger.sebastian.site/schemas/publication/manipulate.html";

// The locale to use
Locale locale = new Locale("en");

// The names of the elements
String versionElement = "Version";
String fileElement = "File";

CmsJspActionElement cmsa = new CmsJspActionElement(pageContext,request,response);
CmsObject cmso = cmsa.getCmsObject();

// read the resource
CmsResource xmlContentResource = cmso.readResource(resource);
CmsFile xmlContentFile = CmsFile.upgrade(xmlContentResource,cmso);

// Output current xml
out.println("&lt;h1&gt;Before&lt;/h1&gt;");
out.println("&lt;pre&gt;" + CmsStringUtil.escapeHtml((new String(xmlContentFile.getContents()))) + "&lt;/pre&gt;");

// build up the xml content instance
CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(cmso,xmlContentFile);

// This is a new version (in this case it is a nested schema
I_CmsXmlContentValue addedVersionValue = xmlContent.addValue(cmso,versionElement ,locale,0);

// Add a new element to the created version (nested schema)
I_CmsXmlContentValue addedFileValueRaw = xmlContent.addValue(
  cmso,addedVersionValue.getPath() + "/" + fileElement,locale,0);
CmsXmlVfsFileValue addedFileValue = (CmsXmlVfsFileValue ) addedFileValueRaw;
addedFileValue.setStringValue(cmso,resource);

// manipulate an element
I_CmsXmlContentValue theSameValueWeAdded = xmlContent.getValue(
  addedVersionValue.getPath() + "/" + fileElement,locale);
// ... do the same as above

// add and remove a value
I_CmsXmlContentValue secondAddedVersionValue = xmlContent.addValue(cmso,
  versionElement,locale,addedVersionValue.getIndex()+1);

out.println("&lt;h1&gt;After adding&lt;/h1&gt;");
out.println("&lt;pre&gt;" + CmsStringUtil.escapeHtml((new String(xmlContent.marshal()))) + "&lt;/pre&gt;");

xmlContent.removeValue(secondAddedVersionValue.getPath(),locale,0);

out.println("&lt;h1&gt;After removing&lt;/h1&gt;");
out.println("&lt;pre&gt;" + CmsStringUtil.escapeHtml((new String(xmlContent.marshal()))) + "&lt;/pre&gt;");

// In the real world you would now do
// xmlContentFile.setContents(xmlContent.marshal());
// cmso.writeResource(xmlContentFile);
%&gt;</pre>
<h3>Version schema</h3>
<pre class="file">&lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"&gt;

       &lt;xsd:include schemaLocation="opencms://opencms-xmlcontent.xsd"/&gt;
       &lt;xsd:element name="XmlContentPublicationVersions"
                    type="OpenCmsXmlContentPublicationVersions"/&gt;

       &lt;xsd:complexType name="OpenCmsXmlContentPublicationVersions"&gt;
               &lt;xsd:sequence&gt;
                       &lt;xsd:element name="XmlContentPublicationVersion"
                                    type="OpenCmsXmlContentPublicationVersion"
                                    minOccurs="0" maxOccurs="unbounded"/&gt;
               &lt;/xsd:sequence&gt;
       &lt;/xsd:complexType&gt;

       &lt;xsd:complexType name="OpenCmsXmlContentPublicationVersion"&gt;
              &lt;xsd:sequence&gt;
                      &lt;xsd:element name="Title" type="OpenCmsString" /&gt;
		      &lt;xsd:element name="PublicationDate" type="OpenCmsDateTime" /&gt;
                      &lt;xsd:element name="Description" type="OpenCmsString" /&gt;
                      &lt;xsd:element name="File" type="OpenCmsVfsFile" minOccurs="0"
                                   maxOccurs="10" /&gt;
              &lt;/xsd:sequence&gt;
              &lt;xsd:attribute name="language" type="OpenCmsLocale" use="optional"/&gt;
       &lt;/xsd:complexType&gt;

&lt;/xsd:schema&gt;</pre>
<h3>Publication schema</h3>
<pre class="file">&lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"&gt;

       &lt;xsd:include schemaLocation="opencms://opencms-xmlcontent.xsd"/&gt;
       &lt;xsd:include
            schemaLocation="opencms://system/modules/de.himberger.sebastian.site/schemas/publication/publication-version.xsd"/&gt;

       &lt;xsd:element name="XmlContentPublications" type="OpenCmsXmlContentPublications"/&gt;

       &lt;xsd:complexType name="OpenCmsXmlContentPublications"&gt;
               &lt;xsd:sequence&gt;
                       &lt;xsd:element name="XmlContentPublication"
                                    type="OpenCmsXmlContentPublication"
                                    minOccurs="0" maxOccurs="unbounded"/&gt;
               &lt;/xsd:sequence&gt;
       &lt;/xsd:complexType&gt;

       &lt;xsd:complexType name="OpenCmsXmlContentPublication"&gt;
              &lt;xsd:sequence&gt;
                      &lt;xsd:element name="Title" type="OpenCmsString" /&gt;
                      &lt;xsd:element name="Subtitle" type="OpenCmsString" /&gt;
                      &lt;xsd:element name="Description" type="OpenCmsHtml" /&gt;
                      &lt;xsd:element name="Version"
                                   type="OpenCmsXmlContentPublicationVersion"
                                   minOccurs="0" maxOccurs="unbounded" /&gt;
              &lt;/xsd:sequence&gt;
              &lt;xsd:attribute name="language" type="OpenCmsLocale" use="required"/&gt;
       &lt;/xsd:complexType&gt;

&lt;/xsd:schema&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.sebastian.himberger.de/blog/2008/12/12/manipulating-opencms-xmlcontent-programatically-some-examples/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

