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.
158 lines
5.6 KiB
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, "name",
|
|
"attr", "value",
|
|
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 "a" element */
|
|
node = mxmlFindElement(tree, tree, "a",
|
|
NULL, NULL,
|
|
MXML_DESCEND);
|
|
</PRE>
|
|
|
|
<!-- NEED 5 -->
|
|
<PRE>
|
|
/* Find the first "a" element with "href"
|
|
attribute */
|
|
node = mxmlFindElement(tree, tree, "a",
|
|
"href", NULL,
|
|
MXML_DESCEND);
|
|
</PRE>
|
|
|
|
<!-- NEED 6 -->
|
|
<PRE>
|
|
/* Find the first "a" element with "href"
|
|
to a URL */
|
|
node = mxmlFindElement(tree, tree, "a",
|
|
"href",
|
|
"http://www.easysw.com/",
|
|
MXML_DESCEND);
|
|
</PRE>
|
|
|
|
<!-- NEED 5 -->
|
|
<PRE>
|
|
/* Find the first element with a "src"
|
|
attribute */
|
|
node = mxmlFindElement(tree, tree, NULL,
|
|
"src", NULL,
|
|
MXML_DESCEND);
|
|
</PRE>
|
|
|
|
<!-- NEED 5 -->
|
|
<PRE>
|
|
/* Find the first element with a "src"
|
|
= "foo.jpg" */
|
|
node = mxmlFindElement(tree, tree, NULL,
|
|
"src", "foo.jpg",
|
|
MXML_DESCEND);
|
|
</PRE>
|
|
<P>You can also iterate with the same function:</P>
|
|
<PRE>
|
|
mxml_node_t *node;
|
|
|
|
for (node = mxmlFindElement(tree, tree,
|
|
"name",
|
|
NULL, NULL,
|
|
MXML_DESCEND);
|
|
node != NULL;
|
|
node = mxmlFindElement(node, tree,
|
|
"name",
|
|
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 "group" would be the "node" element to the
|
|
left, while the next node from "group" would be the "node" 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 "node" and "group" elements under the
|
|
"?xml" 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 "group" would be the "val3"
|
|
node and the next node would be the first node element under "group".
|
|
<P>If you were to walk from the root node "?xml" 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 "val8" and walked using <TT>mxmlWalkPrev()</TT>,
|
|
the order would be reversed, ending at "?xml".</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>
|
|
|