mirror of
https://github.com/michaelrsweet/mxml.git
synced 2024-11-13 23:35:30 +00:00
f367d28d41
Cleanup for comments, articles, strs, and documentation. Fix comments in SQL schema.
128 lines
5.5 KiB
HTML
128 lines
5.5 KiB
HTML
<!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.0</TITLE>
|
|
<META NAME="author" CONTENT="Michael Sweet">
|
|
<META NAME="copyright" CONTENT="Copyright 2003-2004">
|
|
<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="LoadingandSavingXMLFiles.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="LoadingandSavingXMLFiles.html">Previous</A>
|
|
<A HREF="3MoreMiniXMLProgrammingTechniques.html">Next</A>
|
|
<HR NOSHADE>
|
|
<H3><A NAME="3_3_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, "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>
|
|
<PRE>
|
|
/* Find the first "a" element */
|
|
node = <A href="mxmlFindElement.html#mxmlFindElement">mxmlFindElement</A>(tree, tree, "a", NULL, NULL, MXML_DESCEND);
|
|
|
|
/* Find the first "a" element with "href" attribute */
|
|
node = <A href="mxmlFindElement.html#mxmlFindElement">mxmlFindElement</A>(tree, tree, "a", "href", NULL, MXML_DESCEND);
|
|
|
|
/* Find the first "a" element with "href" to a URL */
|
|
node = <A href="mxmlFindElement.html#mxmlFindElement">mxmlFindElement</A>(tree, tree, "a", "href",
|
|
"http://www.easysw.com/~mike/mxml/", MXML_DESCEND);
|
|
|
|
/* Find the first element with a "src" attribute*/
|
|
node = <A href="mxmlFindElement.html#mxmlFindElement">mxmlFindElement</A>(tree, tree, NULL, "src", NULL, MXML_DESCEND);
|
|
|
|
/* Find the first element with a "src" = "foo.jpg" */
|
|
node = <A href="mxmlFindElement.html#mxmlFindElement">mxmlFindElement</A>(tree, tree, NULL, "src", "foo.jpg", 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, "name", NULL, NULL, MXML_DESCEND);
|
|
node != NULL;
|
|
node = <A href="mxmlFindElement.html#mxmlFindElement">mxmlFindElement</A>(node, tree, "name", 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 "group" would be the "node" element to the left, while the
|
|
next node from "group" would be the "node" 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 "node" elements under the "?xml" 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 "group" would be the "val3"
|
|
node and the next node would be the first node element under "group".
|
|
If you were to walk from the root node "?xml" 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 "val9" and walked using <TT>mxmlWalkPrev()</TT>,
|
|
the order would be reversed, ending at "?xml".</P>
|
|
</LI>
|
|
</UL>
|
|
<HR NOSHADE>
|
|
<A HREF="toc.html">Contents</A>
|
|
<A HREF="LoadingandSavingXMLFiles.html">Previous</A>
|
|
<A HREF="3MoreMiniXMLProgrammingTechniques.html">Next</A>
|
|
</BODY>
|
|
</HTML>
|