[opencms-dev] Problem with CmsXmlPageFactory.unmarshal creating only empty pages

Paul-Inge Flakstad flakstad at npolar.no
Fri Feb 12 03:56:19 CET 2010


Hi Eska,

"xmlpage" is just the resource type for "Page with free text".

I'm not totally sure (perhaps someone else can confirm or correct?) but this is what I think:
1.) CmsXmlPage is used to process resources of type "xmlpage".
2.) CmsXmlContent is an equivalent class, used to process structured content resource types ("xmlcontent").

Not sure if you need any more help at this point, but just thought I'd mention it. 

A short example code snippet:

<%@ page import="org.opencms.file.*, 
                 org.opencms.jsp.*, 
                 org.opencms.xml.*, 
                 org.opencms.xml.page.*, 
                 org.opencms.xml.content.*, 
                 org.opencms.xml.types.*,
                 java.util.*" %>
<%
CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
CmsObject cmso = cms.getCmsObject();

String myFile = "/my/structuredcontent/file.html";
Locale locale = new Locale("en");

CmsXmlContent xmlCont = CmsXmlContentFactory.unmarshal(cmso, cmso.readFile(myFile));
out.println("Locales: " + xmlCont.getLocales() + "<br />");

List textElements2 = xmlCont.getNames(locale);
Iterator j = textElements2.iterator();
if (j.hasNext()) {
    out.println("<ul>");
    while (j.hasNext()) {
        String textElementName = (String)j.next();
        String textElement = xmlCont.getValue(textElementName, locale).getStringValue(cmso);
        out.println("<li>" + textElementName + " = " + textElement + "</li>");
    }
    out.println("</ul>");
}
%>

Best regards,
Paul

> -----Original Message-----
> From: opencms-dev-bounces at opencms.org 
> [mailto:opencms-dev-bounces at opencms.org] On Behalf Of Eska
> Sent: 11. februar 2010 16:32
> To: opencms-dev at opencms.org
> Subject: Re: [opencms-dev] Problem with 
> CmsXmlPageFactory.unmarshal creating only empty pages
> 
> 
> Hi Paul-Inge,
> 
> I already tried a way like yours after discovering the 
> "problem" I had, but
> to input the content of one
> CmlXmlContent into another is not so straight-forward as one 
> might think,
> especially with nested content.
> 
> Regarding "xmlpage", is that a special content type and how does one
> create/use one?
> 
> In the meantime I found another solution. I just should have 
> remembered that
> there is a Copy-Locale
> inside of the Content-Editing. It uses the 
> CmsXmlContent#copyLocale(Locale
> source, Locale destination,
> Set elements) method.
> 
> I made myself a quick hack that I can use for copying:
> 
> 
> public static CmsXmlContent copyLocale(CmsObject cmsObj, CmsXmlContent
> contentFrom, CmsXmlContent contentTo, Locale from) throws CmsException
> {
> 	
> 	Document fromDoc = 
> CmsXmlUtils.unmarshalHelper(contentFrom.marshal(), new
> CmsXmlEntityResolver(null));
> 	Document toDoc = 
> CmsXmlUtils.unmarshalHelper(contentTo.marshal(), new
> CmsXmlEntityResolver(null));
> 	
> 	Element fromRoot = fromDoc.getRootElement();
> 	Element toRoot = toDoc.getRootElement();
> 	
>     Element sourceElement = null;
> 
>     Iterator i = fromRoot.elementIterator();
>     String localeStr = from.toString();
>     while (i.hasNext()) {
>         Element element = (Element)i.next();
>         String language =
> element.attributeValue(CmsXmlContentDefinition.XSD_ATTRIBUTE_V
> ALUE_LANGUAGE,
> null);
>         if ((language != null) && (localeStr.equals(language))) {
>             // detach node with the locale
>             sourceElement = element.createCopy();
>             // there can be only one node for the locale
>             break;
>         }
>     }
> 
>     // switch locale value in attribute of copied node
>    
> sourceElement.addAttribute(CmsXmlContentDefinition.XSD_ATTRIBU
> TE_VALUE_LANGUAGE,
> from.toString());
>     
> 
>     // attach the copied node to the root node
>     toRoot.add(sourceElement);    
>     
>     return CmsXmlContentFactory.unmarshal(cmsObj, toDoc,
> contentTo.getEncoding(), new CmsXmlEntityResolver(null));
> 
> }
> 
> The returned CmsXmlContent can now be marshaled to a byte 
> array that can now
> be used to for writing
> my resulting content to the Vfs.
> 
> Eska
> 
> 
> Paul-Inge Flakstad wrote:
> > 
> > Hi Eska
> > 
> > I just tested some equivalent code on my system (OpenCms 
> v7.5.1), with the
> > same result.
> > 
> > I think maybe the class CmsXmlPage is intended for the resource type
> > "xmlpage" and not "xmlcontents" (AKA structured content).
> > 
> > Maybe you can use somehting like this instead, it's what I 
> normally do
> > (although this is a very simplified example, you'll need to 
> look at the
> > docs):
> > 
> > CmsResource res = cmsObj.readResource("/your/file/name");
> > I_CmsXmlDocument xmlDoc = CmsXmlContentFactory.unmarshal(cmso,
> > cmso.readFile(res));
> > I_CmsXmlContentValue val = xmlDoc.getValue(("Headline[1]"), 
> locale); //
> > Index starts at 1 not 0
> > out.println("Headline[1] = " + val.getStringValue(cmsObj));
> > 
> > HTH.
> > 
> > Cheers,
> > Paul
> > 
> >> -----Original Message-----
> >> From: opencms-dev-bounces at opencms.org 
> >> [mailto:opencms-dev-bounces at opencms.org] On Behalf Of Eska
> >> Sent: 11. februar 2010 12:11
> >> To: opencms-dev at opencms.org
> >> Subject: [opencms-dev] Problem with 
> >> CmsXmlPageFactory.unmarshal creating only empty pages
> >> 
> >> 
> >> Hi List,
> >> 
> >> I am trying to program me a little jsp tool that does the same as
> >> CmsMergePages.mergePages(),
> >> but somehow the call to CmsXmlPageFactory.unmarshal() only 
> >> creates empty
> >> CmsXmlPage 
> >> objects for me.
> >> 
> >> 
> >> String resPathDe = "/sites/abc/de/test.html";
> >> String resPathEn = "/sites/abc/en/testa.html"; 
> >> 
> >> CmsFile resFileDe = cmsObj.readFile(resDe);
> >> CmsFile resFileEn = cmsObj.readFile(resEn);
> >> 
> >> Locale de = new Locale("de");
> >> Locale en = new Locale("en");
> >> 
> >> 
> >> CmsXmlPage xmlPageDe = CmsXmlPageFactory.unmarshal(cmsObj, 
> resFileDe);
> >> CmsXmlPage xmlPageEn = CmsXmlPageFactory.unmarshal(cmsObj, 
> resFileEn);
> >> 
> >> Locale locDe = new Locale("de");
> >> Locale locEn = new Locale("en");
> >> 
> >> List textElements2 = xmlPageEn.getNames(locEn );
> >> Iterator j = textElements2.iterator();
> >> while (j.hasNext()) {
> >>    String textElementName = (String)j.next();
> >>    String textElement = xmlPageEn.getValue(textElementName,
> >> locEn).getStringValue(cmsObj);
> >>                     
> >>    if (!xmlPageDe.hasValue(textElementName, locEn)) {
> >>       xmlPageDe.addValue(textElementName, locEn);
> >>    }
> >>    xmlPageDe.setStringValue(cmsObj, textElementName, locEn, 
> >> textElement);
> >> }
> >> ...
> >> 
> >> When now trying to combine both page contents like in the
> >> CmsMergePages.mergePages() method,
> >> an empy content is created only containing only the following:
> >> 
> >> <?xml version="1.0" encoding="UTF-8"?>
> >> 
> >> <pages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> >> xsi:noNamespaceSchemaLocation="http://www.opencms.org/dtd/6.0/
> >> xmlpage.xsd"></pages>
> >> 
> >> 
> >> I gathered the following data:
> >> 
> >> resFileDe.getContents().length: 889
> >> resFileEn.getContents().length: 1024
> >> xmlPageDe.getNames(de).size(): 0
> >> xmlPageEn.getNames(en).size(): 0
> >> xmlPageDe.getValues(de).size(): 0
> >> xmlPageEn.getValues(en).size(): 0
> >> xmlPageDe.getLocales().size(): 0
> >> xmlPageEn.getLocales().size(): 0
> >> textElements2.size()=0
> >> 
> >> My two files have normal content like this:
> >> 
> >> <?xml version="1.0" encoding="UTF-8"?>
> >> 
> >> <ArticlePages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> >> xsi:noNamespaceSchemaLocation="opencms://system/modules/my.tes
> >> t.module/schemas/articlepage.xsd">
> >>   <ArticlePage language="de">
> >>     <Status><![CDATA[7]]></Status>
> >>     <TopProject>false</TopProject>
> >>     <Headline><![CDATA[This is a Headline!]]></Headline>
> >>     <Text name="Text0">
> >> ...
> >> 
> >>    </ArticlePage>
> >> </ArticlePages>
> >> 
> >> Is it the wrong way to try to merge to contents?
> >> 
> >> Thanks,
> >> 
> >> Eska.
> >> -- 
> >> View this message in context: 
> >> http://old.nabble.com/Problem-with-CmsXmlPageFactory.unmarshal
> > -creating-only-empty-pages-tp27544324p27544324.html
> >> Sent from the OpenCMS - Dev mailing list archive at Nabble.com.
> >> 
> >> 
> >> _______________________________________________
> >> 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/mailman/listinfo/opencms-dev
> >> 
> > 
> > _______________________________________________
> > 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/mailman/listinfo/opencms-dev
> > 
> > 
> 
> -- 
> View this message in context: 
> http://old.nabble.com/Problem-with-CmsXmlPageFactory.unmarshal
-creating-only-empty-pages-tp27544324p27547812.html
> Sent from the OpenCMS - Dev mailing list archive at Nabble.com.
> 
> 
> _______________________________________________
> 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/mailman/listinfo/opencms-dev
> 


More information about the opencms-dev mailing list