Version (216 KB) by TADA
Xml export\import for OO-matlab
Updated 23 Oct 2018

View License

MXML package (Matlab-XML) is a standalone package designed for serializing
and deserializing objects(structs\classes) into\from XML\Json files. This format is useful
for transfering a complete object model between Matlab and other working environments,
such as .Net, Java, Javascript, Python, etc.
Parsing DOM objects to structs was taken from:

Main methods:
obj = MXML.load(filePath, [format]) (format = 'json' or 'xml')
obj = MXML.load(xmlOrJsonString, format), obj, [format])

The load\save\toxml methods have a functionality for saving a data object and a metadata object in the same file.
when saving to/loading from file, format is optional, if format is not specified, it will be decided from the file extension:
.json for json format and anything else for xml format

The package has a builtin class factory, which instantiates according to class name.

The general format of the generated XML looks like this:
<document type="struct">
<data type="struct">
<propertyName1 type="propertyType1" [isList="true"]>
<propertyName2 type="cell" isList="true">
<entry type="entryType1" [isList="true"]>[Content]</entry>
<entry type="entryType2" [isList="true"]>[Content]</entry>
<entry type="entryType3" [isList="true"]>[Content]</entry>
<propertyName3 type="propertyType3" [isList="true"]>

Saving to Json format:
The regular object graph will be stripped of data-types once serialized
to JSON format.
In this implementation primitive types such as numerics, logicals,
char-arrays, do not recieve the added typing convention to minimize the
object graph and the json size. This however removes the type
reversibility of these types, i.e, int and double are treated similarly
thus when parsing back from json, int32 types will be parsed into double
and so on.

for instance:
a = struct('id', '', 'children', struct('name', {'a', 'b'}, 'value', {1, 2}), 'class', someClassDef)

jsonizes into the following object graph:
a = struct('id', ''),...
'children', struct('type', 'struct', 'isList', true, 'value', [struct('name', 'a', 'value', 1), struct('name', 'b', 'value', 2)],...
'class', struct('type', 'someClassDef', 'value', struct(all properties of someClassDef))

which in turn is encoded into this json:
{ "id":"",
"children":{"type":"struct", "isList":true, "value":[{"name":"a", "value":1},
{"name":"b", "value":2}]},
"class":{"type":"someClassDef", "value":{"prop1":value1,"prop2":value2,...}} }

Cite As

TADA (2024). MXML (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2015a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Call Web Services from MATLAB Using HTTP in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes

Added serialization to/loading from xml string
Added all functionality for Json format
IIterable no longer sends data struct to ctor, rather now it works as intended, where the data entries are added to the list using the IIterable API.

Description updated again
Description updated