我引用两个老外的代码来说明问题:
如果你使用这一段代码,试图改变一个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
分享到:
相关推荐
一旦XML文件被加载,我们可以使用Linq-to-XML API来解析XML元素和属性。例如,假设我们的XML配置文件如下: ```xml <Item id="1" name="Item1" value="100"/> <Item id="2" name="Item2" value="200"/> ``` ...
UPDATE XMLTable X SET VALUE(X) = XMLType(getCLOBDocument('init.xml')); UPDATE XMLContent SET xmlValue = XMLType(getCLOBDocument('init.xml')) WHERE keyValue = '1'; ``` 注意,这里 `VALUE(X)` 是针对 ...
Debug.Log($"Name: {item.Name}, Value: {item.Value}"); } ``` 总之,Unity3D中的Mono工具通过`System.Xml`提供了XML解析能力,让开发者能够方便地读取、操作XML文件中的数据。无论是使用传统的`XmlDocument`还是...
foreach (XmlNode node in nodes) { var user = new User { Name = node.SelectSingleNode("Name").InnerText, Age = int.Parse(node.SelectSingleNode("Age").InnerText) }; users.Add(user); } return ...
array_to_xml($value, $doc, $childNode); } else { $childNode = $doc->createElement(is_int($key) ? 'item' : $key, $value); $rootNode->appendChild($childNode); } } } ``` 这个`array_to_xml`函数递归...
xmlContent = xmlFile.ReadAll '读取整个文件内容 xmlFile.Close '写入XML文件 Set xmlFile = fileObj.CreateTextFile("C:\new_example.xml", True) xmlFile.WriteLine "<root><element>Value</element></root>" ...
_menuItems = value; OnPropertyChanged(nameof(MenuItems)); } } public void LoadMenuFromXml(string filePath) { var xmlContent = File.ReadAllText(filePath); MenuItems = Menu.ParseXml(xmlContent)...
public static String xmlToJson(String xmlContent) throws IOException { ObjectMapper xmlMapper = new XmlMapper(); return xmlMapper.writeValueAsString(xmlMapper.readTree(xmlContent)); } // 如果...
int index = this.Text.IndexOf(match.Value, StringComparison.OrdinalIgnoreCase); if (index != -1) { int start = index; int end = start + match.Length; this.Select(start, end - start); this....
student.setId(studentElement.attributeValue("id")); student.setName(studentElement.elementText("name")); student.setAge(Integer.parseInt(studentElement.elementText("age"))); // 反射部分 Class<?>...
xmlDoc.Load "path_to_your_xml_file.xml" ``` 之后,可以通过`xmlDoc.getElementsByTagName`或`xmlDoc.SelectSingleNode`等方法遍历和修改XML结构。 2. **XMLHttpRequest对象**: 这个对象允许异步地发送HTTP...
XDocument是LINQ to XML的一部分,它提供了一种更现代且更易用的方式来操作XML。通过LINQ查询,我们可以轻松地找到并打印出所有工作表的名称和ID。 要写入或修改数据,你需要找到对应的工作表XML文件,如"sheet1....
parser.setInput(inputStream, null); // 定义解析过程 int eventType = parser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_TAG) { String tag...
String xmlContent = "<root><element attr='value'>Text</element></root>"; Document document = DocumentHelper.parseText(xmlContent); ``` 2. **获取根元素**:通过`document.getRootElement()`获取XML文档的...
string xmlContent = File.ReadAllText("path_to_xml_file.xml"); XmlSerializer serializer = new XmlSerializer(typeof(PowerConfig)); PowerConfig powerConfig = (PowerConfig)serializer.Deserialize(new ...
parser.setInput(new StringReader(xmlContent)); int eventType = parser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_TAG) { // 处理开始...
metadata = parser.from_file('path_to_your_file', xmlContent=False) for key in metadata: print(f"{key}: {metadata[key]}") # 文件类型检测 file_type = parser.detect('path_to_your_file') print(f"File ...
parser.setInput(new StringReader(xmlContent)); int eventType = parser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_TAG && parser.getName...
Console.WriteLine(iterator.Current.Value); } } ``` **4. 修改XML** `XmlElement`和`XmlNode`提供了一系列方法来添加、删除和修改XML元素及属性。例如,要添加一个新节点: ```csharp XmlElement newNode = ...
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder content = new StringBuilder(); String inputLine; while ((inputLine = in.readLine()) != null) { ...