ar01s04.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Retrieving Element Content</title><meta name="generator" content="DocBook XSL Stylesheets V1.61.2"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="ar01s03.html" title="Parsing the file"><link rel="next" href="ar01s05.html" title="Using XPath to Retrieve Element Content"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Retrieving Element Content</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s03.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ar01s05.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="xmltutorialgettext"></a>Retrieving Element Content</h2></div></div><div></div></div><p><a class="indexterm" name="id2525439"></a>
  2. Retrieving the content of an element involves traversing the document
  3. tree until you find what you are looking for. In this case, we are looking
  4. for an element called "keyword" contained within element called "story". The
  5. process to find the node we are interested in involves tediously walking the
  6. tree. We assume you already have an xmlDocPtr called <tt class="varname">doc</tt>
  7. and an xmlNodPtr called <tt class="varname">cur</tt>.</p><p>
  8. </p><pre class="programlisting">
  9. <a name="getchildnode"></a><img src="images/callouts/1.png" alt="1" border="0">cur = cur-&gt;xmlChildrenNode;
  10. <a name="huntstoryinfo"></a><img src="images/callouts/2.png" alt="2" border="0">while (cur != NULL) {
  11. if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)"storyinfo"))){
  12. parseStory (doc, cur);
  13. }
  14. cur = cur-&gt;next;
  15. }
  16. </pre><p>
  17. </p><div class="calloutlist"><table border="0" summary="Callout list"><tr><td width="5%" valign="top" align="left"><a href="#getchildnode"><img src="images/callouts/1.png" alt="1" border="0"></a> </td><td valign="top" align="left"><p>Get the first child node of <tt class="varname">cur</tt>. At this
  18. point, <tt class="varname">cur</tt> points at the document root, which is
  19. the element "story".</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#huntstoryinfo"><img src="images/callouts/2.png" alt="2" border="0"></a> </td><td valign="top" align="left"><p>This loop iterates through the elements that are children of
  20. "story", looking for one called "storyinfo". That
  21. is the element that will contain the "keywords" we are
  22. looking for. It uses the <span class="application">libxml</span> string
  23. comparison
  24. function, <tt class="function"><a href="http://xmlsoft.org/html/libxml-parser.html#XMLSTRCMP" target="_top">xmlStrcmp</a></tt>. If there is a match, it calls the function <tt class="function">parseStory</tt>.</p></td></tr></table></div><p>
  25. </p><p>
  26. </p><pre class="programlisting">
  27. void
  28. parseStory (xmlDocPtr doc, xmlNodePtr cur) {
  29. xmlChar *key;
  30. <a name="anothergetchild"></a><img src="images/callouts/1.png" alt="1" border="0"> cur = cur-&gt;xmlChildrenNode;
  31. <a name="findkeyword"></a><img src="images/callouts/2.png" alt="2" border="0"> while (cur != NULL) {
  32. if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)"keyword"))) {
  33. <a name="foundkeyword"></a><img src="images/callouts/3.png" alt="3" border="0"> key = xmlNodeListGetString(doc, cur-&gt;xmlChildrenNode, 1);
  34. printf("keyword: %s\n", key);
  35. xmlFree(key);
  36. }
  37. cur = cur-&gt;next;
  38. }
  39. return;
  40. }
  41. </pre><p>
  42. </p><div class="calloutlist"><table border="0" summary="Callout list"><tr><td width="5%" valign="top" align="left"><a href="#anothergetchild"><img src="images/callouts/1.png" alt="1" border="0"></a> </td><td valign="top" align="left"><p>Again we get the first child node.</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#findkeyword"><img src="images/callouts/2.png" alt="2" border="0"></a> </td><td valign="top" align="left"><p>Like the loop above, we then iterate through the nodes, looking
  43. for one that matches the element we're interested in, in this case
  44. "keyword".</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#foundkeyword"><img src="images/callouts/3.png" alt="3" border="0"></a> </td><td valign="top" align="left"><p>When we find the "keyword" element, we need to print
  45. its contents. Remember that in <span class="acronym">XML</span>, the text
  46. contained within an element is a child node of that element, so we
  47. turn to <tt class="varname">cur-&gt;xmlChildrenNode</tt>. To retrieve it, we
  48. use the function <tt class="function"><a href="http://xmlsoft.org/html/libxml-tree.html#XMLNODELISTGETSTRING" target="_top">xmlNodeListGetString</a></tt>, which also takes the <tt class="varname">doc</tt> pointer as an argument. In this case, we just print it out.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td colspan="2" align="left" valign="top"><p>Because <tt class="function">xmlNodeListGetString</tt> allocates
  49. memory for the string it returns, you must use
  50. <tt class="function">xmlFree</tt> to free it.</p></td></tr></table></div></td></tr></table></div><p>
  51. </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s03.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ar01s05.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Parsing the file </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Using XPath to Retrieve Element Content</td></tr></table></div></body></html>