You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
mxml/www/docfiles/FindingandIteratingNodes.html

127 lines
5.5 KiB

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<TITLE>Mini-XML Programmers Manual, Version 2.2.1</TITLE>
<META NAME="author" CONTENT="Michael Sweet">
<META NAME="copyright" CONTENT="Copyright 2003-2005">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=iso-iso-8859-1">
<LINK REL="Start" HREF="index.html">
<LINK REL="Contents" HREF="toc.html">
<LINK REL="Prev" HREF="SavingXML.html">
<LINK REL="Next" HREF="3MoreMiniXMLProgrammingTechniques.html">
<STYLE TYPE="text/css"><!--
BODY { font-family: serif }
H1 { font-family: sans-serif }
H2 { font-family: sans-serif }
H3 { font-family: sans-serif }
H4 { font-family: sans-serif }
H5 { font-family: sans-serif }
H6 { font-family: sans-serif }
SUB { font-size: smaller }
SUP { font-size: smaller }
PRE { font-family: monospace }
--></STYLE>
</HEAD>
<BODY>
<A HREF="toc.html">Contents</A>
<A HREF="SavingXML.html">Previous</A>
<A HREF="3MoreMiniXMLProgrammingTechniques.html">Next</A>
<HR NOSHADE>
<H3><A NAME="3_4_1">Finding and Iterating Nodes</A></H3>
<P>The <A href="mxmlWalkPrev.html#mxmlWalkPrev"><TT>mxmlWalkPrev()</TT></A>
and <A href="mxmlWalkNext.html#mxmlWalkNext"><TT>mxmlWalkNext()</TT></A>
functions can be used to iterate through the XML node tree:</P>
<PRE>
<A href="mxmlnodet.html#mxml_node_t">mxml_node_t</A> *node = <A href="mxmlWalkPrev.html#mxmlWalkPrev">mxmlWalkPrev</A>(current, tree, MXML_DESCEND);
<A href="mxmlnodet.html#mxml_node_t">mxml_node_t</A> *node = <A href="mxmlWalkNext.html#mxmlWalkNext">mxmlWalkNext</A>(current, tree, MXML_DESCEND);
</PRE>
<P>In addition, you can find a named element/node using the <A href="mxmlFindElement.html#mxmlFindElement">
<TT>mxmlFindElement()</TT></A> function:</P>
<PRE>
<A href="mxmlnodet.html#mxml_node_t">mxml_node_t</A> *node = <A href="mxmlFindElement.html#mxmlFindElement">mxmlFindElement</A>(tree, tree, &quot;name&quot;, &quot;attr&quot;,
&quot;value&quot;, MXML_DESCEND);
</PRE>
<P>The <TT>name</TT>, <TT>attr</TT>, and <TT>value</TT> arguments can be
passed as <TT>NULL</TT> to act as wildcards, e.g.:</P>
<PRE>
/* Find the first &quot;a&quot; element */
node = <A href="mxmlFindElement.html#mxmlFindElement">mxmlFindElement</A>(tree, tree, &quot;a&quot;, NULL, NULL, MXML_DESCEND);
/* Find the first &quot;a&quot; element with &quot;href&quot; attribute */
node = <A href="mxmlFindElement.html#mxmlFindElement">mxmlFindElement</A>(tree, tree, &quot;a&quot;, &quot;href&quot;, NULL, MXML_DESCEND);
/* Find the first &quot;a&quot; element with &quot;href&quot; to a URL */
node = <A href="mxmlFindElement.html#mxmlFindElement">mxmlFindElement</A>(tree, tree, &quot;a&quot;, &quot;href&quot;,
&quot;http://www.easysw.com/~mike/mxml/&quot;, MXML_DESCEND);
/* Find the first element with a &quot;src&quot; attribute*/
node = <A href="mxmlFindElement.html#mxmlFindElement">mxmlFindElement</A>(tree, tree, NULL, &quot;src&quot;, NULL, MXML_DESCEND);
/* Find the first element with a &quot;src&quot; = &quot;foo.jpg&quot; */
node = <A href="mxmlFindElement.html#mxmlFindElement">mxmlFindElement</A>(tree, tree, NULL, &quot;src&quot;, &quot;foo.jpg&quot;, MXML_DESCEND);
</PRE>
<P>You can also iterate with the same function:</P>
<PRE>
<A href="mxmlnodet.html#mxml_node_t">mxml_node_t</A> *node;
for (node = <A href="mxmlFindElement.html#mxmlFindElement">mxmlFindElement</A>(tree, tree, &quot;name&quot;, NULL, NULL, MXML_DESCEND);
node != NULL;
node = <A href="mxmlFindElement.html#mxmlFindElement">mxmlFindElement</A>(node, tree, &quot;name&quot;, NULL, NULL, MXML_DESCEND))
{
... do something ...
}
</PRE>
<P>The <TT>MXML_DESCEND</TT> argument can actually be one of three
constants:</P>
<UL>
<LI><TT>MXML_NO_DESCEND</TT> means to not to look at any child nodes in
the element hierarchy, just look at siblings at the same level or
parent nodes until the top node or top-of-tree is reached. The previous
node from &quot;group&quot; would be the &quot;node&quot; element to the left, while the
next node from &quot;group&quot; would be the &quot;node&quot; element to the right.</LI>
<LI><TT>MXML_DESCEND_FIRST</TT> means that it is OK to descend to the
first child of a node, but not to descend further when searching.
You'll normally use this when iterating through direct children of a
parent node, e.g. all of the &quot;node&quot; elements under the &quot;?xml&quot; parent
node in the example above. This mode is only applicable to the search
function; the walk functions treat this as <TT>MXML_DESCEND</TT> since
every call is a first time.</LI>
<LI><TT>MXML_DESCEND</TT> means to keep descending until you hit the
bottom of the tree. The previous node from &quot;group&quot; would be the &quot;val3&quot;
node and the next node would be the first node element under &quot;group&quot;.
If you were to walk from the root node &quot;?xml&quot; to the end of the tree
with <TT>mxmlWalkNext()</TT>, the order would be:
<PRE>
?xml
data
node
val1
node
val2
node
val3
group
node
val4
node
val5
node
val6
node
val7
node
val8
node
val9
</PRE>
<P>If you started at &quot;val9&quot; and walked using <TT>mxmlWalkPrev()</TT>,
the order would be reversed, ending at &quot;?xml&quot;.</P>
</LI>
</UL>
<HR NOSHADE>
<A HREF="toc.html">Contents</A>
<A HREF="SavingXML.html">Previous</A>
<A HREF="3MoreMiniXMLProgrammingTechniques.html">Next</A>
</BODY>
</HTML>