File Exchange

image thumbnail

xml2struct , with bug fix and added features

version 1.0.0.0 (4.88 KB) by CY Y

CY Y (view profile)

An improved version of xml2struct that converts xml document into MATLAB structure.

71 Downloads

Updated 09 Mar 2017

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 : https://www.mathworks.com/matlabcentral/fileexchange/28518-xml2struct .

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.

Comments and Ratings (27)

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!

BINH LE

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

Jan L (view profile)

showing unexpected MATLAB '<' Operator for the following xml file
https://msdn.microsoft.com/en-us/library/ms762271(v=vs.85).aspx

john

john (view profile)

Kresten

M.Abuasbeh

Thanks Joe Yeh!
Very helpful!

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.

CY Y

CY Y (view profile)

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.

KAE

KAE (view profile)

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

Arun M (view profile)

How to deal with the following error?

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

Wee-Lih Lee

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))';

SACHIT VARMA

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_

CY Y

CY Y (view profile)

Thank you. Very useful !

CY Y

CY Y (view profile)

@ 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 https://fr.mathworks.com/matlabcentral/fileexchange/28639-struct2xml crash during execution

CY Y

CY Y (view profile)

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 _ .

R S

R S (view profile)

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

CY Y

CY Y (view profile)

@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!

Updates

1.0.0.0

Fixed the " Undefined function 'toCharArray' for input arguments of type 'double'. " issue.

1.0.0.0

updated description

1.0.0.0

Updated title

1.0.0.0

Updated description.

1.0.0.0

linked to github

1.0.0.0

added tags

1.0.0.0

Minor changes in code comment

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

Inspired by: xml2struct

Inspired: EnergyPlus co-simulation toolbox

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.


Learn About Live Editor