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