File Exchange

image thumbnail

xml2struct , with bug fix and added features

version (3.17 KB) by CY Y
An improved version of xml2struct that converts xml document into MATLAB structure.


Updated 09 Mar 2017

From GitHub

View license on GitHub

xml2struct takes either a java xml object, an xml file, or a string in xml format and returns a structure that preserves the tree relationship in the xml document. It is an improved version of another MATLAB file exchange submission : .

Specifically, it has following features:
1. Solved the issue that when comment is present, other data will be lost.
2. Fixed the " Undefined function 'toCharArray' for input arguments of type 'double'. " issue.
3. Added support for xml string.

Cite As

CY Y (2021). xml2struct , with bug fix and added features (, GitHub. Retrieved .

Comments and Ratings (35)

Adam Rubock

Hint: if you need to iterate over a struct that has cell arrays inside, (i.e. the format generated by this function,) use the structtree function here:

Adam Rubock

Claudio Wilmanns

@Jeremiah Brown (your comment on 31 Dec 2019):
I stumbled upon the same issue.
The xml2struct version here (upadeted 09 Mar 2017) contains those lines that result in the issue as earlier replacements of '-', ':' and '.' will result in a new "name" variable containing the underline character '_' which then get replaced by 'u_'.
% ----- Local function getNodeData -----
function [text,name,attr,childs,textflag] = getNodeData(theNode)
% Create structure of node info.
%make sure name is allowed as structure name
name = char(getNodeName(theNode));
name = strrep(name, '-', '_dash_');
name = strrep(name, ':', '_colon_');
name = strrep(name, '.', '_dot_');
name = strrep(name, '_', 'u_');

In my case simply changing the order to replace any occurence of the underline character '_' gets replaced first, so:
name = char(getNodeName(theNode));
name = strrep(name, '_', 'u_');
name = strrep(name, '-', '_dash_');
name = strrep(name, ':', '_colon_');
name = strrep(name, '.', '_dot_');

But your snippet seems to have done that re-ordered replacement already which you commented out.
So even with the re-ordered replacement, you still get that issue?
What happens if the node name contains the underscore '_' character? It works without issues?

@Chao-Yuan Yeh: Can you fix that issue with the underscore character, please? That would be great. Thanks.

Alois Primes

Marcin Konowalczyk

Jeremiah Brown

This is very helpful. One issue I ran into was that our XML files include names containing '_', and those were all replaced by "u_". In the getNodeData function, I commented out line 138 and added code to only look at the first character of name and prepend 'u' if name starts with '_':
% name = strrep(name, '_', 'u_');
name = strrep(name, '-', '_dash_');
name = strrep(name, ':', '_colon_');
name = strrep(name, '.', '_dot_');
if name(1) == '_'
name = append('u',name);

Hongxiao Jin

Kiarash Ahi

I wonder if there is any python equivalent of this script?

Jiayan Liu

Gernot Druml

Thank you for solving the " Undefined function 'toCharArray' for input arguments of type 'double'. " issue.

It saves me from editing hundreds of files per hand!


Thank you very much for such a great tool.
BTW, when convert XML element's name to match MATLAB struct field name requirements,
name = strrep(name, '_', 'u_'); should be the first before '-', ':' , '.' to avoid unintentional effect.
E.g: "a:int" turns into "au_colon_int" instead of "a_colon_int".

Jan L

Siddharth singh

showing unexpected MATLAB '<' Operator for the following xml file




Thanks Joe Yeh!
Very helpful!

subhasis adhikari

How do I get data from the output structure file? I have an XML file containing an image file. I want to read this XML file which I can do it using this program. Now, I want to make the image but I don't know how to get my data from the structure file which I get using this program.

Timofey Goloborodko


to Mark Tolman and Arun M, I think this issue should be resolved now. I've changed toCharArray() to char(), which should always be present in MATLAB.


I tried this when the example in the xmlread documentation didn't work for my XML file, and this did, so thanks. I also found the output structure's organization to be easy to work with and understand.

Arun M

How to deal with the following error?

Undefined function 'toCharArray' for input arguments of type 'double'.

Wee-Lih Lee

Michael Helmberger

Darshan Shah

great help

Mark Tolman

I'm getting the following error. Should I by importing something prior to using this script? How do I fix it?

Undefined function 'toCharArray' for input arguments of type 'double'.

Error in xml2struct_fixed>getNodeData (line 153)
text.(textflag) = toCharArray(getTextContent(theNode))';


Very useful !
Just one problem, the fields in my xml file are not starting with a _ but are in the middle. Still the _ are being replaced by a u_


Thank you. Very useful !


@ Herve , I don't have such script available but I think the solution you posted on the other file exchange entry is a valid one.

Herve Demezil

is there a file to revert the operation compatible with R2016a? The one in crash during execution


Thanks R S for pointing this out. Now the leading _ in the node name will be replaced with u_ . Field name of a MATLAB struct can not begin with an _ .


There is an error when node name begins with '_'. Try loading MS Visual Studio (2010) project vcxproj file.


@Ian, thanks for the comment. I've fixed the issue.

Ian Craig

There is an error on line 79 (I opened a git issue on it), but otherwise very well done.

Yoann Ladroit

Looks robust. Rather fast as well! REally useful to me thanks very much!

MATLAB Release Compatibility
Created with R2016b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: xml2struct

Inspired: EnergyPlus Co-simulation Toolbox

Community Treasure Hunt

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

Start Hunting!