0

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')

And my XML file :

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

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

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?

EDIT

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);

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

Edit 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')

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

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

m_power
  • 3,156
  • 5
  • 33
  • 54

1 Answers1

0

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);

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

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137
  • If you print the node value after updating it, does it actually changed? looks like you're setting different node from the node you displayed before, no? – har07 Aug 20 '14 at 12:50
  • You are right. I printed the value after updating it and it was still the same. But even with this change, it is still not saved. See my second edit. – m_power Aug 20 '14 at 12:57
  • Just to make sure, does the 2nd edit followed by the 1st edit (saving back to file) when you tested? – har07 Aug 20 '14 at 13:26
  • 1
    Yes, my 2nd edit include the code from the 1st edit. Now, when I display the node value after changing it, I see that it is correctly changed. – m_power Aug 20 '14 at 13:36
  • your code using `java.io.StringWriter()`, but the thread I linked above use a `File()`. If I'm not mistaken, you need to save result of `writer.ToString()` to the XML file manually at the end then... – har07 Aug 28 '14 at 04:43