Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
reading an xml file

Subject: reading an xml file

From: Juan Ruiz

Date: 10 May, 2003 14:48:10

Message: 1 of 6

Hello:

Does anybody know a simple tutorial or a simple example to read the elements
and attributes from an xml file?
I am using matlab 6.5 and xmlread.

I can read the file and obtain a DOM object but then how I get the values
inside?
3 or four lines of code to understand the idea would be perfect for me. (for
instance reading the info.xml
file included in matlab)

I have not found examples in the matlab documentation

Thanks.

Subject: reading an xml file

From: Doug Schwarz

Date: 11 May, 2003 03:39:04

Message: 2 of 6

In article <b9js2t$jogqq$1@ID-145658.news.dfncis.de>,
 "Juan Ruiz" <jjruiz@nps.navy.mil> wrote:

> Hello:
>
> Does anybody know a simple tutorial or a simple example to read the elements
> and attributes from an xml file?
> I am using matlab 6.5 and xmlread.
>
> I can read the file and obtain a DOM object but then how I get the values
> inside?
> 3 or four lines of code to understand the idea would be perfect for me. (for
> instance reading the info.xml
> file included in matlab)
>
> I have not found examples in the matlab documentation
>
> Thanks.


Always glad to help the Navy. :-)

This function calls xmlread and then processes the DOM node to make a
MATLAB structure with the same tree structure as the DOM node. You may
or may not find that to be useful, but it contains code to process the
DOM node so it should be educational.

You can call methods(<any Java object>) to get the names of the methods
that are applicable to that object. Use that to experiment. There are
many ways to extract the information from the DOM node.


------------------------- xml2struct.m ------------------------
function out = xml2struct(xmlfile)
% XML2STRUCT Read an XML file into a MATLAB structure.

% written By Douglas M. Schwarz, douglas.schwarz@kodak.com

xml = xmlread(xmlfile);

children = xml.getChildNodes;
for i = 1:children.getLength
   out(i) = node2struct(children.item(i-1));
end



function s = node2struct(node)

s.name = char(node.getNodeName);

if node.hasAttributes
   attributes = node.getAttributes;
   nattr = attributes.getLength;
   s.attributes = struct('name',cell(1,nattr),'value',cell(1,nattr));
   for i = 1:nattr
      attr = attributes.item(i-1);
      s.attributes(i).name = char(attr.getName);
      s.attributes(i).value = char(attr.getValue);
   end
else
   s.attributes = [];
end

try
   s.data = char(node.getData);
catch
   s.data = '';
end

if node.hasChildNodes
   children = node.getChildNodes;
   nchildren = children.getLength;
   c = cell(1,nchildren);
   s.children = struct('name',c,'attributes',c,'data',c,'children',c);
   for i = 1:nchildren
      child = children.item(i-1);
      s.children(i) = node2struct(child);
   end
else
   s.children = [];
end
----------------------------------------------------------------

--
Doug Schwarz
Eastman Kodak Company
douglas.schwarz@kodak.com

Subject: reading an xml file

From: Juan Ruiz

Date: 11 May, 2003 14:17:01

Message: 3 of 6

Thanks for helping me understand the idea.

Juan.

"Doug Schwarz" <douglas.schwarz@kodak.com> wrote in message
news:douglas.schwarz-994ADB.23393010052003@news.frontiernet.net...
> In article <b9js2t$jogqq$1@ID-145658.news.dfncis.de>,
> "Juan Ruiz" <jjruiz@nps.navy.mil> wrote:
>
> > Hello:
> >
> > Does anybody know a simple tutorial or a simple example to read the
elements
> > and attributes from an xml file?
> > I am using matlab 6.5 and xmlread.
> >
> > I can read the file and obtain a DOM object but then how I get the
values
> > inside?
> > 3 or four lines of code to understand the idea would be perfect for me.
(for
> > instance reading the info.xml
> > file included in matlab)
> >
> > I have not found examples in the matlab documentation
> >
> > Thanks.
>
>
> Always glad to help the Navy. :-)
>
> This function calls xmlread and then processes the DOM node to make a
> MATLAB structure with the same tree structure as the DOM node. You may
> or may not find that to be useful, but it contains code to process the
> DOM node so it should be educational.
>
> You can call methods(<any Java object>) to get the names of the methods
> that are applicable to that object. Use that to experiment. There are
> many ways to extract the information from the DOM node.
>
>
> ------------------------- xml2struct.m ------------------------
> function out = xml2struct(xmlfile)
> % XML2STRUCT Read an XML file into a MATLAB structure.
>
> % written By Douglas M. Schwarz, douglas.schwarz@kodak.com
>
> xml = xmlread(xmlfile);
>
> children = xml.getChildNodes;
> for i = 1:children.getLength
> out(i) = node2struct(children.item(i-1));
> end
>
>
>
> function s = node2struct(node)
>
> s.name = char(node.getNodeName);
>
> if node.hasAttributes
> attributes = node.getAttributes;
> nattr = attributes.getLength;
> s.attributes = struct('name',cell(1,nattr),'value',cell(1,nattr));
> for i = 1:nattr
> attr = attributes.item(i-1);
> s.attributes(i).name = char(attr.getName);
> s.attributes(i).value = char(attr.getValue);
> end
> else
> s.attributes = [];
> end
>
> try
> s.data = char(node.getData);
> catch
> s.data = '';
> end
>
> if node.hasChildNodes
> children = node.getChildNodes;
> nchildren = children.getLength;
> c = cell(1,nchildren);
> s.children = struct('name',c,'attributes',c,'data',c,'children',c);
> for i = 1:nchildren
> child = children.item(i-1);
> s.children(i) = node2struct(child);
> end
> else
> s.children = [];
> end
> ----------------------------------------------------------------
>
> --
> Doug Schwarz
> Eastman Kodak Company
> douglas.schwarz@kodak.com

Subject: reading an xml file

From: Emiliya

Date: 17 Aug, 2012 10:21:07

Message: 4 of 6

Hello,
i wrote a function which generates a xml file:

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
   <NumberOfBaseStations>3</NumberOfBaseStations>
   <BaseStations>
      <BS ID="0" X="0" Y="0"/>
      <BS ID="1" X="0" Y="0"/>
      <BS ID="2" X="0" Y="0"/>
   </BaseStations>
</Configuration>

I need a function which reads this XML file and saves the information in a Matlab struct. I have tried the posted code and it works fine. I cannot understand why there are children with the name "#text" in the Matlab struct ?

Subject: reading an xml file

From: Bruno Luong

Date: 17 Aug, 2012 10:59:07

Message: 5 of 6

May be you should try xml2struct on FEX

http://www.mathworks.com/matlabcentral/fileexchange/28518-xml2struct

Bruno

Subject: reading an xml file

From: TideMan

Date: 17 Aug, 2012 21:14:21

Message: 6 of 6

On Friday, August 17, 2012 10:59:07 PM UTC+12, Bruno Luong wrote:
> May be you should try xml2struct on FEX
>
>
>
> http://www.mathworks.com/matlabcentral/fileexchange/28518-xml2struct
>
>
>
> Bruno

I endorse Bruno's suggestion.
xml2struct is the program to use.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us