Hej,
I just noticed, that the xml_read function does not detect arrays correctly from attributes of xml-nodes.
I use the str2num=smart default option. I found, that there is an error in the str2var function on line 452 where I just removed the square brackets from the digit array:
digits = '(Inf)|(NaN)|(pi)|[\t\n\d\+\-\*\.ei EI\;\,]';
Now it works again like a charm. Great work by the way. Hope you can add the bugfix to a future release.
Daniel
Comment only
22 Dec 2011
xml_io_tools
Read XML files into MATLAB struct and writes MATLAB data types to XML
Jaroslaw,
Thanks for this excellent work. I enjoy using it, so I wanted to share what I did to possibly improve "xml_read".
Reading in the following .xml file:
<?xml version="1.0" encoding="utf-8"?>
<ROOT>
<xtr_base>
<item>0x30010</item>
</xtr_base>
<reg_base>0x30000</reg_base>
<pcm_base>
<item>0x30040</item>
<item>0x30050</item>
</pcm_base>
</ROOT>
I performed the following commands:
>> D3=xml_read('testrdsmall_jec.xml');
>> D3
D3 =
xtr_base: '0x30010'
reg_base: '0x30000'
pcm_base: {'0x30040' '0x30050'}
It bothered me that the resulting data structure in Matlab has both xtr_base and reg_base the same. I tried using Pref.CellItem=true, as in the following commands:
>> Pref.CellItem=true;
>> D2=xml_read('testrdsmall_jec.xml',Pref);
>> D2
D2 =
xtr_base: {'0x30010'}
reg_base: '0x30000'
pcm_base: {{1x2 cell}}
This did produce a difference between xtr_base and reg_base, but the pcm_base was now a cell-array within a cell-array! So, I decided to make a modification to your code...
So now, with my small modification, I do the following commands:
>> D5=xml_read_jec('testrdsmall_jec.xml');
>> D5
D5 =
xtr_base: {'0x30010'}
reg_base: '0x30000'
pcm_base: {'0x30040' '0x30050'}
It produces the result I expect. However, when I use Pref.CellItem=true, I get:
>> D=xml_read_jec('testrdsmall_jec.xml',Pref);
>> D
D =
xtr_base: {'0x30010'}
reg_base: '0x30000'
pcm_base: {{1x2 cell}}
... which is probably not exactly as it should be. So, maybe my modification is not entirely correct.
What I did was to change lines 383-387 from this:
if (isfield(s,Pref.ItemName))
s.CONTENT = s.(Pref.ItemName);
s = rmfield(s,Pref.ItemName);
ItemContent = Pref.CellItem; % if CellItem than keep s.CONTENT as cells
end
to be this instead:
if (isfield(s,Pref.ItemName))
% Check for singleton nodes that have the special keyword to mark arrays,
% and keep them as cell arrays, even though the resulting cell array
% only has one member. -- Modification by John Clayton, 12/22/11.
if ~iscell(s.(Pref.ItemName))
s.CONTENT = s.(Pref.ItemName);
s = rmfield(s,Pref.ItemName);
ItemContent = true;
else
s.CONTENT = s.(Pref.ItemName);
s = rmfield(s,Pref.ItemName);
ItemContent = Pref.CellItem; % if CellItem then keep s.CONTENT as cells
end
end
What do you think of this change? Perhaps I did it not very well, but I wanted to tell you about it.
Thanks again for your excellent work!
- John Clayton
5
14 Dec 2011
xml_io_tools
Read XML files into MATLAB struct and writes MATLAB data types to XML
Comment only