1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" |
---|
2 | "http://www.w3.org/TR/REC-html40/loose.dtd"> |
---|
3 | <html> |
---|
4 | <head> |
---|
5 | <title>Description of parseXML</title> |
---|
6 | <meta name="keywords" content="parseXML"> |
---|
7 | <meta name="description" content="PARSEXML Convert XML file to a MATLAB structure. (from matlab help)"> |
---|
8 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
---|
9 | <meta name="generator" content="m2html © 2003 Guillaume Flandin"> |
---|
10 | <meta name="robots" content="index, follow"> |
---|
11 | <link type="text/css" rel="stylesheet" href="../m2html.css"> |
---|
12 | </head> |
---|
13 | <body> |
---|
14 | <a name="_top"></a> |
---|
15 | <div><a href="../index.html">Home</a> > <a href="index.html">.</a> > parseXML.m</div> |
---|
16 | |
---|
17 | <!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png"> Master index</a></td> |
---|
18 | <td align="right"><a href="index.html">Index for . <img alt=">" border="0" src="../right.png"></a></td></tr></table>--> |
---|
19 | |
---|
20 | <h1>parseXML |
---|
21 | </h1> |
---|
22 | |
---|
23 | <h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
24 | <div class="box"><strong>PARSEXML Convert XML file to a MATLAB structure. (from matlab help)</strong></div> |
---|
25 | |
---|
26 | <h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
27 | <div class="box"><strong>function theStruct = parseXML(filename) </strong></div> |
---|
28 | |
---|
29 | <h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
30 | <div class="fragment"><pre class="comment"> PARSEXML Convert XML file to a MATLAB structure. (from matlab help)</pre></div> |
---|
31 | |
---|
32 | <!-- crossreference --> |
---|
33 | <h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
34 | This function calls: |
---|
35 | <ul style="list-style-image:url(../matlabicon.gif)"> |
---|
36 | </ul> |
---|
37 | This function is called by: |
---|
38 | <ul style="list-style-image:url(../matlabicon.gif)"> |
---|
39 | </ul> |
---|
40 | <!-- crossreference --> |
---|
41 | |
---|
42 | <h2><a name="_subfunctions"></a>SUBFUNCTIONS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
43 | <ul style="list-style-image:url(../matlabicon.gif)"> |
---|
44 | <li><a href="#_sub1" class="code">function children = parseChildNodes(theNode)</a></li><li><a href="#_sub2" class="code">function nodeStruct = makeStructFromNode(theNode)</a></li><li><a href="#_sub3" class="code">function attributes = parseAttributes(theNode)</a></li></ul> |
---|
45 | <h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2> |
---|
46 | <div class="fragment"><pre>0001 <span class="comment">% PARSEXML Convert XML file to a MATLAB structure. (from matlab help)</span> |
---|
47 | 0002 <a name="_sub0" href="#_subfunctions" class="code">function theStruct = parseXML(filename)</a> |
---|
48 | 0003 |
---|
49 | 0004 <span class="keyword">try</span> |
---|
50 | 0005 tree = xmlread(filename); |
---|
51 | 0006 <span class="keyword">catch</span> |
---|
52 | 0007 error(<span class="string">'Failed to read XML file %s.'</span>,filename); |
---|
53 | 0008 <span class="keyword">end</span> |
---|
54 | 0009 |
---|
55 | 0010 <span class="comment">% Recurse over child nodes. This could run into problems</span> |
---|
56 | 0011 <span class="comment">% with very deeply nested trees.</span> |
---|
57 | 0012 <span class="keyword">try</span> |
---|
58 | 0013 theStruct = <a href="#_sub1" class="code" title="subfunction children = parseChildNodes(theNode)">parseChildNodes</a>(tree); |
---|
59 | 0014 <span class="keyword">catch</span> |
---|
60 | 0015 error(<span class="string">'Unable to parse XML file %s.'</span>); |
---|
61 | 0016 <span class="keyword">end</span> |
---|
62 | 0017 |
---|
63 | 0018 |
---|
64 | 0019 <span class="comment">% ----- Subfunction PARSECHILDNODES -----</span> |
---|
65 | 0020 <a name="_sub1" href="#_subfunctions" class="code">function children = parseChildNodes(theNode)</a> |
---|
66 | 0021 <span class="comment">% Recurse over node children.</span> |
---|
67 | 0022 children = []; |
---|
68 | 0023 <span class="keyword">if</span> theNode.hasChildNodes |
---|
69 | 0024 childNodes = theNode.getChildNodes; |
---|
70 | 0025 numChildNodes = childNodes.getLength; |
---|
71 | 0026 allocCell = cell(1, numChildNodes); |
---|
72 | 0027 |
---|
73 | 0028 children = struct( <span class="keyword">...</span> |
---|
74 | 0029 <span class="string">'Name'</span>, allocCell, <span class="string">'Attributes'</span>, allocCell, <span class="keyword">...</span> |
---|
75 | 0030 <span class="string">'Data'</span>, allocCell, <span class="string">'Children'</span>, allocCell); |
---|
76 | 0031 |
---|
77 | 0032 <span class="keyword">for</span> count = 1:numChildNodes |
---|
78 | 0033 theChild = childNodes.item(count-1); |
---|
79 | 0034 children(count) = <a href="#_sub2" class="code" title="subfunction nodeStruct = makeStructFromNode(theNode)">makeStructFromNode</a>(theChild); |
---|
80 | 0035 <span class="keyword">end</span> |
---|
81 | 0036 <span class="keyword">end</span> |
---|
82 | 0037 |
---|
83 | 0038 <span class="comment">% ----- Subfunction MAKESTRUCTFROMNODE -----</span> |
---|
84 | 0039 <a name="_sub2" href="#_subfunctions" class="code">function nodeStruct = makeStructFromNode(theNode)</a> |
---|
85 | 0040 <span class="comment">% Create structure of node info.</span> |
---|
86 | 0041 |
---|
87 | 0042 nodeStruct = struct( <span class="keyword">...</span> |
---|
88 | 0043 <span class="string">'Name'</span>, char(theNode.getNodeName), <span class="keyword">...</span> |
---|
89 | 0044 <span class="string">'Attributes'</span>, <a href="#_sub3" class="code" title="subfunction attributes = parseAttributes(theNode)">parseAttributes</a>(theNode), <span class="keyword">...</span> |
---|
90 | 0045 <span class="string">'Data'</span>, <span class="string">''</span>, <span class="keyword">...</span> |
---|
91 | 0046 <span class="string">'Children'</span>, <a href="#_sub1" class="code" title="subfunction children = parseChildNodes(theNode)">parseChildNodes</a>(theNode)); |
---|
92 | 0047 |
---|
93 | 0048 <span class="keyword">if</span> any(strcmp(methods(theNode), <span class="string">'getData'</span>)) |
---|
94 | 0049 nodeStruct.Data = char(theNode.getData); |
---|
95 | 0050 <span class="keyword">else</span> |
---|
96 | 0051 nodeStruct.Data = <span class="string">''</span>; |
---|
97 | 0052 <span class="keyword">end</span> |
---|
98 | 0053 |
---|
99 | 0054 <span class="comment">% ----- Subfunction PARSEATTRIBUTES -----</span> |
---|
100 | 0055 <a name="_sub3" href="#_subfunctions" class="code">function attributes = parseAttributes(theNode)</a> |
---|
101 | 0056 <span class="comment">% Create attributes structure.</span> |
---|
102 | 0057 |
---|
103 | 0058 attributes = []; |
---|
104 | 0059 <span class="keyword">if</span> theNode.hasAttributes |
---|
105 | 0060 theAttributes = theNode.getAttributes; |
---|
106 | 0061 numAttributes = theAttributes.getLength; |
---|
107 | 0062 allocCell = cell(1, numAttributes); |
---|
108 | 0063 attributes = struct(<span class="string">'Name'</span>, allocCell, <span class="string">'Value'</span>, allocCell); |
---|
109 | 0064 |
---|
110 | 0065 <span class="keyword">for</span> count = 1:numAttributes |
---|
111 | 0066 attrib = theAttributes.item(count-1); |
---|
112 | 0067 attributes(count).Name = char(attrib.getName); |
---|
113 | 0068 attributes(count).Value = char(attrib.getValue); |
---|
114 | 0069 <span class="keyword">end</span> |
---|
115 | 0070 <span class="keyword">end</span></pre></div> |
---|
116 | <hr><address>Generated on Fri 13-Nov-2009 11:17:03 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> © 2003</address> |
---|
117 | </body> |
---|
118 | </html> |
---|