From: Tom Hughes on 30 Mar 2010 16:00 Can anyone explain to me how to parse an xml file in Matlab? I can use xmlread to input the DOM node but how do I get to the data that is contained in the file? Here is a sample file that I've been trying to work with. <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="web" cover="paperback"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
From: Doug Schwarz on 30 Mar 2010 16:20 Tom Hughes wrote: > Can anyone explain to me how to parse an xml file in Matlab? I can use > xmlread to input the DOM node but how do I get to the data that is > contained in the file? Here is a sample file that I've been trying to > work with. [snip] Maybe this will help. ---------------------- xml2struct.m ---------------------- function out = xml2struct(xmlfile) %XML2STRUCT Read XML file into a structure. % Douglas M. Schwarz 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 dmschwarz&ieee,org Make obvious changes to get real email address.
|
Pages: 1 Prev: classregtree node split order Next: Logical Vectors From Values |