MATLAB: Error with xml files and Matlab structures

matlab structurestruct2xmlstructurexmlxml2struct

I am trying to modify an xml file through Matlab.
In order to do it, I am using this to convert the xml file into a Matlab structure and this other one to get the xml file back.
The problem is: I encounter an error when I try to convert the Matlab structure back into an xml file. The error is:
if true
Error using xmlwrite (line 82)
Java exception occurred:
java.lang.NullPointerException
at com.mathworks.xml.XMLUtils.serializeXML(XMLUtils.java:192)
at com.mathworks.xml.XMLUtils.serializeXML(XMLUtils.java:49)
Error in struct2xml (line 79)
varargout{1} = xmlwrite(docNode);
Error in read_change_timestamps (line 5)
test.xml = struct2xml(eci);
end
It has to do with the second function "struct2xml", but I can't seem to understand what exactly is the problem.
This is an example of an xml file that I'm trying to modify:
if true
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<eventTrack xmlns="http://www.egi.com/event_mff" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>ECI TCP/IP 55513</name>
<trackType>STIM</trackType>
<event>
<beginTime>2017-04-04T14:15:43.529000+00:00</beginTime>
<duration>1000</duration>
<code>Task</code>
<label></label>
<description></description>
<sourceDevice>Multi-Port ECI</sourceDevice>
<keys>
<key>
<keyCode>dura</keyCode>
<data dataType="short">147</data>
</key>
</keys>
</event>
<event>
<beginTime>2017-04-04T14:15:45.979000+00:00</beginTime>
<duration>1000</duration>
<code>stim</code>
<label></label>
<description></description>
<sourceDevice>Multi-Port ECI</sourceDevice>
<keys>
<key>
<keyCode>tria</keyCode>
<data dataType="short">1</data>
</key>
<key>
<keyCode>code</keyCode>
<data dataType="short">70</data>
</key>
<key>
<keyCode>type</keyCode>
<data dataType="short">2</data>
</key>
</keys>
</event>
<event>
<beginTime>2017-04-04T14:15:46.096000+00:00</beginTime>
<duration>1000</duration>
<code>ISI </code>
<label></label>
<description></description>
<sourceDevice>Multi-Port ECI</sourceDevice>
<keys>
<key>
<keyCode>dura</keyCode>
<data dataType="short">164</data>
</key>
</keys>
</event>
<event>
<beginTime>2017-04-04T14:15:48.830000+00:00</beginTime>
<duration>1000</duration>
<code>stim</code>
<label></label>
<description></description>
<sourceDevice>Multi-Port ECI</sourceDevice>
<keys>
<key>
<keyCode>tria</keyCode>
<data dataType="short">2</data>
</key>
<key>
<keyCode>code</keyCode>
<data dataType="short">149</data>
</key>
<key>
<keyCode>type</keyCode>
<data dataType="short">1</data>
</key>
</keys>
</event>
<event>
<beginTime>2017-04-04T14:15:48.930000+00:00</beginTime>
<duration>1000</duration>
<code>ISI </code>
<label></label>
<description></description>
<sourceDevice>Multi-Port ECI</sourceDevice>
<keys>
<key>
<keyCode>dura</keyCode>
<data dataType="short">159</data>
</key>
</keys>
</event>
<event>
<beginTime>2017-04-04T14:15:51.580000+00:00</beginTime>
<duration>1000</duration>
<code>stim</code>
<label></label>
<description></description>
<sourceDevice>Multi-Port ECI</sourceDevice>
<keys>
<key>
<keyCode>tria</keyCode>
<data dataType="short">3</data>
</key>
<key>
<keyCode>code</keyCode>
<data dataType="short">66</data>
</key>
<key>
<keyCode>type</keyCode>
<data dataType="short">2</data>
</key>
</keys>
</event>
<event>
<beginTime>2017-04-04T14:15:51.680000+00:00</beginTime>
<duration>1000</duration>
<code>ISI </code>
<label></label>
<description></description>
<sourceDevice>Multi-Port ECI</sourceDevice>
<keys>
<key>
<keyCode>dura</keyCode>
<data dataType="short">147</data>
</key>
</keys>
</event>
</eventTrack>
end
What could possibly be the problem with it? Is it because of the structure of the original xml file?

Best Answer

I'm not sure if it's a bug in matlab xml functions or in struct2xml but the problem is caused by nodes with empty text. struct2xml sets the text property of these nodes to [] instead of '' which matlab xml functions do not like. If I remember correctly, matlab used to treat both of these empty matrix the same, but is now a bit more strict about it.
It's a very simple fix. On line 166, of struct2xml, change
str = [];
to
str = '';
edit: And I've just noticed that the problem and fix is actually mentioned on the struct2xml page.
edit2: Note that if all you want to do is change the time stamps of some of the node, converting the xml to struct and back is a waste of time. You would be better off learning how to navigate the DOM using the object returned by xmlread. It would be faster and would avoid the risk that either conversion fail.