can handle:
- real and complex matrices: scalars, 1D, 2D, N-D arrays
- struct arrays
- cell arrays
- function handles
- nesting of structures/cells
NOT supported: objects, xml attributes (are used for types and sizes)
usage:
tinyxml2_wrap('save', filename, variable)
tinyxml2_wrap('save', filename, variable, exportOptions)
variable = tinyxml2_wrap('load', filename)
... parse, format, version modes: see help
compile with:
mex tinyxml2_wrap.cpp tinyxml2.cpp
performance:
on average up to 10x speedup over using XML Toolbox by Marc Molinari
Ladislav Dobrovsky (2021). tinyxml2 wrapper (https://www.mathworks.com/matlabcentral/fileexchange/48308-tinyxml2-wrapper), MATLAB Central File Exchange. Retrieved .
Inspired by: XML Toolbox
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
A simple workaround for the full path issue is to get the file's full path using "which" before calling tinyxml2_wrap.
I uncovered a small difference between XML toolbox and this excellent replacement:
xml_load will find files on the matlab search path however, tinyxml2 will not, and requires the full path of the file to be specified.
The problem was quite hard to diagnose, this was the error when the full path was not specified, but the file was on the matlab search path:
Error using tinyxml2_wrap
failed reading file "test.xml" ; (null)
I am not able to compile the MEX file on Linux Debian 9 64bit (MATLAB R2018b):
>> mex tinyxml2_wrap.cpp
Building with 'g++'.
Error using mex
/tmp/mex_209904070577212_3384/tinyxml2_wrap.o: In function `addChar(tinyxml2::XMLNode*, char const*, mxArray_tag const*, char const*, ExportOptions&)':
tinyxml2_wrap.cpp:(.text+0xdd): undefined reference to `tinyxml2::XMLDocument::NewElement(char const*)'
tinyxml2_wrap.cpp:(.text+0xeb): undefined reference to `tinyxml2::XMLNode::InsertEndChild(tinyxml2::XMLNode*)'
tinyxml2_wrap.cpp:(.text+0x12d): undefined reference to `tinyxml2::XMLElement::FindOrCreateAttribute(char const*)'
tinyxml2_wrap.cpp:(.text+0x138): undefined reference to `tinyxml2::XMLAttribute::SetAttribute(char const*)'
tinyxml2_wrap.cpp:(.text+0x14f): undefined reference to `tinyxml2::XMLDocument::NewText(char const*)'
tinyxml2_wrap.cpp:(.text+0x15a): undefined reference to `tinyxml2::XMLNode::InsertFirstChild(tinyxml2::XMLNode*)'
tinyxml2_wrap.cpp:(.text+0x218): undefined reference to `tinyxml2::XMLElement::FindOrCreateAttribute(char const*)'
tinyxml2_wrap.cpp:(.text+0x223): undefined reference to `tinyxml2::XMLAttribute::SetAttribute(char const*)'
...
Any help???
Another Issue has popped up: there are certain characters, that are not parsed correctly. In German there is äöü and ß as special characters. Those don't survive the parsing. In the XML they are correct. Any Ideas?
Thank you for this brilliant piece of software!
I have issues with fieldnames that are not valid matlab fieldnames: if the xml contains fields that contain e.g. dots ('some.fieldname') in the fieldname the structure in Matlab also contains those fields, but they are not adressable (Matlab throughs an error)
Any Ideas how to fix this?
Thomas
Hello,
Thank you for making this wonderful replacement for XML Toolbox.
In the process of migrating to it, I have been running the tests that were in the XML Toolbox function, xml_tests.m
I discovered that tests involving saving a variable of all spaces fail.
For example if we create a variable that is all spaces, save it to a file test.xml, then reload it, the spaces are squashed and the return variable is an empty char rather than all spaces.
dat = ' '
tinyxml2_wrap('save','test.xml',dat)
dat2 = tinyxml2_wrap('load','test.xml')
cmp = strcmp(dat,dat2)
In this example we end up with dat2 = '', and cmp = 0
Any hints or suggestions how to fix this?
This is clearly not a major issue but would be nice to fix if possible.
Thank you,
Matt
This is a great drop-in replacement for the old xml_toolbox.
I do suggest adding the following lines to the getClassByName class in misc_utils.h for backwards compatibility with the old xml_toolbox.
else if (!strcmp("boolean", name))
return mxLOGICAL_CLASS;
Thanks!
This is a great tool and a great replacement to the old xml-toolbox. it is both flexible and efficient.
to be able to read xml files that were previously written in the old toolbox I have replaced line 1009 in tinyxml2_wrap.cpp with the following code to handle logical values
mxClassID classID;
if(strcmp(classStr,"boolean")==0) {
classID = mxLOGICAL_CLASS;
}
else {
classID = Utils::getClassByName(classStr);
}
Thank you for this tool. Works great (and fast).
It looks a nice project but I need the scanning of the attribute so it is not useful to me. Sorry
Great code helped a lot.
Even with the hint in the options for changing the printf modification when storing double or float, binary precision is never reached for float (maybe also for double) due to disambiguity between decimal representation and float representation.
printf has the option for "%a" to store as hex values.
For loading the line where stringstream reads back in tinyxml2_wrap one can then have an exception for double and float like:
ss >> std::hexfloat >> data[i];
this allows for reading values that were stored in hexadecimal format. Worked fine for me and now also the least significant bit is always same as when loading from *.mat
maybe this is helpful for others.
Have you considered adding compatibility for Matlab tables? The code would be very similar to that for structs. This would be a great improvement.
Ahmed> you have add it to your path and compile with mex, also you need to install supported C++ compiler for your version of MATLAB
How can I use this tool plz
I download it then I did install but nothing happened
Thanks, a very useful tool, is there any option to ignore non-native classes to escape the error: unsupported class to save in xml format ?
Is there an already compiled version of this. Not everyone has access to a compiler in Matlab.
Nice to see a successor to the good old XML_toolbox.
Would it be possible to remove the dependency on the Symbolic Math Toolbox and Simulink, as many Matlab users may only have Matlab and perhaps one toolbox that they for their field of work.
Thanks, I had tried to fix it myself but this way looks better.
Cees Lambregts: problem with struct has been solved (test_simple)
Peter van den Biggelaar: thank you for Your contribution with structs
Peter van den Biggelaar: thank You very much for your feedback, please see the changes;
I have included Your script (with acknowledgement) in tests directory.
uint8, uint8 and uint32 data types are not handled correctly.
The number of digits printed when saving double and single precision numbers is not enough.
Example:
data.int8 = int8(-127:128);
data.uint8 = uint8(0:255);
data.uint32 = uint32(4000000000);
data.double = 2-eps;
data.single = 2-eps('single');
tinyxml2_wrap('save', 'mydata.xml', data);
data2 = tinyxml2_wrap('load', 'mydata.xml');
% plot is expected to show a straight line but it is garbaged:
figure; plot(data.int8, data2.int8);
figure; plot(data.uint8, data2.uint8);
% next values are not as expected:
data2.uint32 % expecting 4000000000
data2.double-2 % expecting 2.2204e-16
data2.single-2 % expecting 1.1921e-07
The uint32 issue can be fixed by changing line 477 in tinymxl2_wrap.cpp:
case mxUINT32_CLASS: return extract<unsigned int>(element, classID)
The accuracy issue can be fixed by changing lines 584 and 585:
case mxDOUBLE_CLASS: return "%.17le";
case mxSINGLE_CLASS: return "%.7e";
Have not found out how to fix the int8 and uint8 issue.
CL: struct arrays have here different format with metadata; it could be interesting to support this format, but it would require to analyze xml; now is the algorithm very straight forward.
http://www.w3schools.com/xml/simple.xml
tinyxml_wrap('load',simple.xml) renders a 1x1 struct, with 1 food element (and not 5).
CL:
structs arrays work fine:
http://pastebin.com/C07ymp24
please provide example of what does not work
Super! Just what I needed to replace xml_toolbox. And so fast!
Works very well, however when n structs are supplied, only the n'th struct is outputted. Is there any eta on this? (As the wrapper has some TODO tags)