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

158 lines
5.6 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.3</TITLE>
<META NAME="author" CONTENT="Michael R. Sweet">
<META NAME="copyright" CONTENT="Copyright 2003-2007">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=iso-iso-8859-1">
<LINK REL="Start" HREF="index.html">
<LINK REL="Contents" HREF="index.html">
<LINK REL="Prev" HREF="ControllingLineWrapping.html">
<LINK REL="Next" HREF="MoreMiniXMLProgrammingTechniques.html">
<STYLE TYPE="text/css"><!--
BODY { font-family: sans-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 }
A { text-decoration: none }
--></STYLE>
</HEAD>
<BODY>
<A HREF="index.html">Contents</A>
<A HREF="ControllingLineWrapping.html">Previous</A>
<A HREF="MoreMiniXMLProgrammingTechniques.html">Next</A>
<HR NOSHADE>
<H2><A NAME="3_6">Finding and Iterating Nodes</A></H2>
<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>
mxml_node_t *node;
node = mxmlWalkPrev(current, tree,
MXML_DESCEND);
node = mxmlWalkNext(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>
mxml_node_t *node;
node = mxmlFindElement(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>
<!-- NEED 4 -->
<PRE>
/* Find the first &quot;a&quot; element */
node = mxmlFindElement(tree, tree, &quot;a&quot;,
NULL, NULL,
MXML_DESCEND);
</PRE>
<!-- NEED 5 -->
<PRE>
/* Find the first &quot;a&quot; element with &quot;href&quot;
attribute */
node = mxmlFindElement(tree, tree, &quot;a&quot;,
&quot;href&quot;, NULL,
MXML_DESCEND);
</PRE>
<!-- NEED 6 -->
<PRE>
/* Find the first &quot;a&quot; element with &quot;href&quot;
to a URL */
node = mxmlFindElement(tree, tree, &quot;a&quot;,
&quot;href&quot;,
&quot;http://www.easysw.com/&quot;,
MXML_DESCEND);
</PRE>
<!-- NEED 5 -->
<PRE>
/* Find the first element with a &quot;src&quot;
attribute */
node = mxmlFindElement(tree, tree, NULL,
&quot;src&quot;, NULL,
MXML_DESCEND);
</PRE>
<!-- NEED 5 -->
<PRE>
/* Find the first element with a &quot;src&quot;
= &quot;foo.jpg&quot; */
node = mxmlFindElement(tree, tree, NULL,
&quot;src&quot;, &quot;foo.jpg&quot;,
MXML_DESCEND);
</PRE>
<P>You can also iterate with the same function:</P>
<PRE>
mxml_node_t *node;
for (node = mxmlFindElement(tree, tree,
&quot;name&quot;,
NULL, NULL,
MXML_DESCEND);
node != NULL;
node = mxmlFindElement(node, tree,
&quot;name&quot;,
NULL, NULL,
MXML_DESCEND))
{
... do something ...
}
</PRE>
<!-- NEED 10 -->
<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.
<P>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.
<BR>
<BR></P>
</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; and &quot;group&quot; elements under the
&quot;?xml&quot; parent node in the example above.
<P>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.
<BR>
<BR></P>
</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;.
<P>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:</P>
<P><TT>?xml data node val1 node val2 node val3 group node val4 node val5
node val6 node val7 node val8</TT></P>
<P>If you started at &quot;val8&quot; and walked using <TT>mxmlWalkPrev()</TT>,
the order would be reversed, ending at &quot;?xml&quot;.</P>
</LI>
</UL>
<HR NOSHADE>
<A HREF="index.html">Contents</A>
<A HREF="ControllingLineWrapping.html">Previous</A>
<A HREF="MoreMiniXMLProgrammingTechniques.html">Next</A>
</BODY>
</HTML>