2008-03-20

How to set a value in a xmlcontent resource

关键字: how to set a value in a xmlcontent resource programtically

我引用两个老外的代码来说明问题:

如果你使用这一段代码,试图改变一个xmlcontent的内容的话,你不会成功

CmsResource document = (CmsResource) cms.getCmsObject().readResource(xmlcontentPath);

CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(cms.getCmsObject(), document, request);

xmlContent.getValue("Date", cms.getRequestContext().getLocale()).setStringValue(cms.getCmsObject(), String.valueOf(Calendar.getInstance(new java.util.Locale("es")).getTimeInMillis()));

cms.getCmsObject().writeFile(xmlContent.getFile());

为什么?因为你必须先把修改好的xmlcontent的字节码重新写回到CmsFile对象里面去:

//you need to insert a line like this before you write the XML content:
//The reason is that the underlying file object of the XML content is not automatically updated when you modify the XML content values.

xmlContent.getFile().setContents(xmlContent.marshal());

 不过根据我实践的结果,光是这一点还不够,你在修改文件前,还要锁定文件:

/* lock this xmldoc, must be done before save */
			cmsobject.lockResource(fileName);

如果还是报can not read resource的异常,八成是你的siteroot不对,这时在程序最前面加上:

/* set site root is important before read xml doc, or you can not read resource from '/' */
		cmso.getRequestContext().setSiteRoot("/");

  

老外讨论的原文:http://www.nabble.com/How-to-set-a-value-in-a-xmlcontent-resource-td13809143.html

 

 

 

 

 

评论
cai555 2008-03-21
原来你就是那个燕子钨,呵呵
wangyi878750 2008-03-21
操作任何文件之前,必须先得到所,才有可能进行操作。
发表评论

您还没有登录,请登录后发表评论

cai555
搜索本博客
最近加入圈子
存档
最新评论