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
发表评论
- 浏览: 12152 次

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
POI拷贝Sheet包括每个单 ...
你好,你写的这个方法正式我说需要的,不过我有2个问题:1、该方法能实现两个不同的 ...
-- by jackini -
自己写OpenCms自定义结构 ...
老兄,这是按页面关键字进行搜索,你知道怎么按网页创建的时间进行搜索吗.急啊!!! ...
-- by zhouxiao315 -
opencms7.0.x plugin for ...
太好了,有更新了
-- by cai555 -
opencms7.0.x plugin for ...
我的环境:Myeclipse6.0(eclipse3.3.1)/openCMS7 ...
-- by foolpcman -
谁能告诉我为什么parseInt ...
居然被评为入门帖,不过还是感谢解答的各位
-- by cai555






评论排行榜