Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

reading an xml file

106 views
Skip to first unread message

Juan Ruiz

unread,
May 10, 2003, 5:48:10 PM5/10/03
to
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.


Doug Schwarz

unread,
May 10, 2003, 11:39:04 PM5/10/03
to
In article <b9js2t$jogqq$1...@ID-145658.news.dfncis.de>,
"Juan Ruiz" <jjr...@nps.navy.mil> wrote:


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...@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...@kodak.com

Juan Ruiz

unread,
May 11, 2003, 5:17:01 PM5/11/03
to
Thanks for helping me understand the idea.

Juan.

"Doug Schwarz" <douglas...@kodak.com> wrote in message
news:douglas.schwarz-99...@news.frontiernet.net...

Emiliya

unread,
Aug 17, 2012, 6:21:07 AM8/17/12
to
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 ?

Bruno Luong

unread,
Aug 17, 2012, 6:59:07 AM8/17/12
to
May be you should try xml2struct on FEX

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

Bruno

TideMan

unread,
Aug 17, 2012, 5:14:21 PM8/17/12
to
I endorse Bruno's suggestion.
xml2struct is the program to use.
0 new messages