在MATLAB中使用XPath修改XML文件 [英] Modify an XML file with XPath in MATLAB

查看:321
本文介绍了在MATLAB中使用XPath修改XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用XPath在MATLAB中打开和修改XML文件.这里是我到目前为止编写的代码:

I'm trying to open and modify an XML file in MATLAB using XPath. Here the code I have written so far:

import javax.xml.xpath.*
doc = xmlread(which('myXMLfile.xml'));

factory = XPathFactory.newInstance();
xpath = factory.newXPath();
expr = xpath.compile('/data//parameter[@name=''MYPARAMETER'']/double');

nodeList = expr.evaluate(doc,XPathConstants.NODESET);
disp(char(nodeList.item(0).getFirstChild.getNodeValue))

nodeList.item(0).setNodeValue('0.03')

还有我的XML文件:

<data>
...
  <parameter name="MYPARAMETER">
    <double>0.05</double>
  </parameter>
...

disp行在MATLAB命令窗口中正确显示了值,这里是0.05.

The disp line correctly display in the MATLAB command window the value, which here is 0.05.

该脚本不会引发错误.但是,未在XML文件中设置0.03值.我在做什么错了,为什么用setNodeValue命令没有将值写入文件?

The script doesn't throw an error. However, the 0.03 value is not set in the XML file. What am I doing wrong and why is the value not written to the file with the setNodeValue command?

编辑

如所建议的,它可能不会被保存,因为它只是在内存中被修改了.我在代码中添加了以下几行:

As proposed, it might not be saved because it is just modified in memory. I added the following lines to my code:

factory = javax.xml.transform.TransformerFactory.newInstance();
transformer = factory.newTransformer();

writer = java.io.StringWriter();
result = javax.xml.transform.stream.StreamResult(writer);
source = javax.xml.transform.dom.DOMSource(doc);

transformer.transform(source, result);

我没有收到任何错误,但是XML文件仍然没有被修改.

I'm getting no errors, but the XML file is still not modified.

编辑2

import javax.xml.xpath.*
import javax.xml.transform.*
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

doc = xmlread(which('ImagingSensor.vpar'));

factory = XPathFactory.newInstance();
xpath = factory.newXPath();
expr = xpath.compile('/data//parameter[@name=''FOV'']/double');

nodeList = expr.evaluate(doc,XPathConstants.NODESET);
disp(char(nodeList.item(0).getFirstChild.getNodeValue))

nodeList.item(0).getFirstChild.setNodeValue('0.03')

使用nodeList.item(0).getFirstChild.setNodeValue('0.03')可以正确更改值(但仍未保存到文件中).

With nodeList.item(0).getFirstChild.setNodeValue('0.03') the value is correctly changed (but still not saved to file).

使用nodeList.item(0).setNodeValue('0.03')时,它没有正确更改值.

When using nodeList.item(0).setNodeValue('0.03'), it didn't change the value correctly.

推荐答案

似乎您只在修改内存中的XML文档对象.最后尝试将对象保存回XML文件.像这样的东西*:

Seems that you're modifying only the XML document object in memory. Try to save the object back to XML file at the end. Something like this maybe* :

Transformer transformer = TransformerFactory.newInstance().newTransformer();
Result output = new StreamResult(new File("myXMLfile.xml"));
Source input = new DOMSource(myDocument);

transformer.transform(input, output);

*)我不是Java专家,摘录自该线程:

*) I'm not a Java guy, snippet taken from this thread : How to save parsed and changed DOM document in xml file?

这篇关于在MATLAB中使用XPath修改XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆