How to set a value in a xmlcontent resource

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

如果你使用这一段代码,试图改变一个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

 

 

 

 

 

你可能感兴趣的:(html,cms,xml)