Code covered by the BSD License  

Highlights from
xml2struct

4.92308

4.9 | 14 ratings Rate this file 176 Downloads (last 30 days) File Size: 2.94 KB File ID: #28518

xml2struct

by Wouter Falkena

 

20 Aug 2010 (Updated 15 May 2012)

Convert an xml file into a MATLAB structure for easy access to the data.

| Watch this File

File Information
Description

Convert an xml file into a MATLAB structure for easy access to the data.

MATLAB release MATLAB 7.9 (2009b)
Other requirements xmlread
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (17)
31 Aug 2010 Yanai  
26 Sep 2010 TideMan

I was tearing my hair out trying to figure out how to automatically access one tiny piece of data in a .xml file until I found this routine.

03 Nov 2010 Joanne

Worked on the first try for loading an OSM data file.

22 Nov 2010 Mathieu

Warning: all XML files haven't '.xml' extension

22 Nov 2010 Wouter Falkena

You are correct. I have removed the '.xml' extension assumption, unless the file can not be found. The update file is currently under review by MATLAB Central and should appear here soon.

24 Nov 2010 Krishnan Suresh

Thanks v. much! I used it to read a Collada file (geometry file Google Sketch-up). Worked like a charm!

05 Mar 2011 Joao Henriques

Simple and works pretty well! The structures are a bit verbose but they're supposed to be parsed by my program anyway; any attempts to collapse some of the nested structures would only slow down the code (some similar submissions do this but are much slower). Thanks!

01 Apr 2011 Bernard

Thanks a lot! I finally came across a tool that can extract info from a ISO19115/19139 xml file.

22 May 2011 Mark

Thanks, your auto field naming system worked great for me to work with data parsed out from XML files.

14 Jun 2011 Adrian Wanner

Thanks for your work.
You might want to speed up the attribute parsing by about 40% by replacing lines 152-154 by the following:
str=theAttributes.item(count-1).toString.toCharArray()';
k=strfind(str,'=');
attr_name = regexprep(str(1:(k(1)-1)),'[-:.]','_');
attributes.(attr_name) =str((k(1)+2):(end-1));

04 Jul 2011 Wouter Falkena

Thank you for this suggestion Mr. Wanner. I have updated the file and it is currently under review by the MATLAB Central. It will appear here shortly.

12 Sep 2011 Brad  
18 Jan 2012 Kevin Moerman  
18 Jan 2012 Kevin Moerman

Works great for small files. I tested it for some larger files with >100000 entries and this takes around 178 seconds.

27 Feb 2012 ali

Excellent! I was pulling my hair to read to numbers from XML file and with this I did it in one minute

20 Mar 2012 Ivan Smirnov

One of the problems that I personally encountered is that xml2struct can't handle CDATA blocks.

It can be easily fixed, replace line 67 with:
            if (~strcmp(name,'#text') && ~strcmp(name,'#comment') && ~strcmp(name,'#cdata_dash_section'))
and line 94 with:
            elseif (strcmp(name,'#text') || strcmp(name, '#cdata_dash_section'))

Works great otherwise, thanks.

12 May 2012 Xiaohu  
Please login to add a comment or rating.
Updates
21 Aug 2010

Decreased the processing time for large XML files

22 Nov 2010

Removed the assumption that the filename should have a '.xml' extension

24 Nov 2010

Corrected the uploaded file

04 Jul 2011

Attribute parsing speed increased by 40%

02 Jan 2012

The function now replaces element and attribute names containing - by _dash_, . by _dot_ and : by _colon_

14 May 2012

Speed improvement due to X. Mo and added support for cdata and comments.

15 May 2012

Small bugfix in the CDATA and Comment structure fields.

Tag Activity for this File
Tag Applied By Date/Time
xml Wouter Falkena 20 Aug 2010 10:15:11
xml2struct Wouter Falkena 20 Aug 2010 10:15:11
struct Wouter Falkena 20 Aug 2010 10:15:12
xml Mathieu CABANES 15 Apr 2011 04:28:27
xml ali 27 Feb 2012 11:43:31
xml silvia 14 May 2012 09:25:17
struct Mengmeng Zhang 01 Jun 2012 10:26:52
xml2struct Mengmeng Zhang 01 Jun 2012 10:26:53

Contact us at files@mathworks.com