Error with xml files and Matlab structures

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?

2 Comments

Either it's a bug in struct2xml or there's something wrong with the structure you're trying to write.
Either way, we need that structure to find out, so attach it to your post as a mat file.
Also, which version of matlab are you using?
I am using Matlab 2016b.
For now I'm not trying to do anything complicated with the structures, just trying to open it from the xml files and get another xml file back.
This is the code:

Sign in to comment.

 Accepted Answer

Guillaume
Guillaume on 16 May 2017
Edited: Guillaume on 16 May 2017
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.

1 Comment

My fault, I didn't notice that there was a fix in the comment section.
I tried what you said, and the problem was solved.
Thank you!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!