diff options
| author | MDT 2002 John Fleck <jfleck@inkstain.net> | 2002-06-04 15:10:36 +0000 |
|---|---|---|
| committer | John Fleck <jfleck@src.gnome.org> | 2002-06-04 15:10:36 +0000 |
| commit | 598f6ebb94b00e6583f4b330dcad08ab3a0c5a36 (patch) | |
| tree | b53a2e9533f3373486db82e84b872d7a6d06aaf7 /doc | |
| parent | 6f4e5e7697b6a0d034a476b2da677a7bbec165d4 (diff) | |
| download | android_external_libxml2-598f6ebb94b00e6583f4b330dcad08ab3a0c5a36.tar.gz android_external_libxml2-598f6ebb94b00e6583f4b330dcad08ab3a0c5a36.tar.bz2 android_external_libxml2-598f6ebb94b00e6583f4b330dcad08ab3a0c5a36.zip | |
apa.html apb.html apc.html apd.html ar01s02.html ar01s03.html ar01s04.html
Tue Jun 4 09:09:18 MDT 2002 John Fleck <jfleck@inkstain.net>
* added doc/tutorial, including:
apa.html
apb.html
apc.html
apd.html
ar01s02.html
ar01s03.html
ar01s04.html
ar01s05.html
ar01s06.html
includeaddattribute.c
includeaddkeyword.c
includekeyword.c
includestory.xml
index.html
xmltutorial.xml
libxml tutorial, including generated html
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/tutorial/apa.html | 15 | ||||
| -rw-r--r-- | doc/tutorial/apb.html | 78 | ||||
| -rw-r--r-- | doc/tutorial/apc.html | 77 | ||||
| -rw-r--r-- | doc/tutorial/apd.html | 67 | ||||
| -rw-r--r-- | doc/tutorial/ar01s02.html | 11 | ||||
| -rw-r--r-- | doc/tutorial/ar01s03.html | 33 | ||||
| -rw-r--r-- | doc/tutorial/ar01s04.html | 50 | ||||
| -rw-r--r-- | doc/tutorial/ar01s05.html | 34 | ||||
| -rw-r--r-- | doc/tutorial/ar01s06.html | 30 | ||||
| -rw-r--r-- | doc/tutorial/includeaddattribute.c | 63 | ||||
| -rw-r--r-- | doc/tutorial/includeaddkeyword.c | 73 | ||||
| -rw-r--r-- | doc/tutorial/includekeyword.c | 74 | ||||
| -rw-r--r-- | doc/tutorial/includestory.xml | 14 | ||||
| -rw-r--r-- | doc/tutorial/index.html | 16 | ||||
| -rw-r--r-- | doc/tutorial/xmltutorial.xml | 372 |
15 files changed, 1007 insertions, 0 deletions
diff --git a/doc/tutorial/apa.html b/doc/tutorial/apa.html new file mode 100644 index 00000000..d01ea2ed --- /dev/null +++ b/doc/tutorial/apa.html @@ -0,0 +1,15 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> +<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>A. Sample Document</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="ar01s06.html" title="Writing Attribute"><link rel="next" href="apb.html" title="B. Code for Keyword Example"></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">A. Sample Document</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s06.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="apb.html">Next</a></td></tr></table><hr></div><div class="appendix"><h2 class="title" style="clear: both"><a name="sampledoc"></a>A. Sample Document</h2><pre class="programlisting"> +<?xml version="1.0"?> +<story> + <storyinfo> + <author>John Fleck</author> + <datewritten>June 2, 2002</datewritten> + <keyword>example keyword</keyword> + </storyinfo> + <body> + <headline>This is the headline</headline> + <para>This is the body text.</para> + </body> +</story> +</pre></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s06.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="apb.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Writing Attribute </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> B. Code for Keyword Example</td></tr></table></div></body></html> diff --git a/doc/tutorial/apb.html b/doc/tutorial/apb.html new file mode 100644 index 00000000..e3b119ff --- /dev/null +++ b/doc/tutorial/apb.html @@ -0,0 +1,78 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> +<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>B. Code for Keyword Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="apa.html" title="A. Sample Document"><link rel="next" href="apc.html" title="C. Code for Add Keyword Example"></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">B. Code for Keyword Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apa.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="apc.html">Next</a></td></tr></table><hr></div><div class="appendix"><h2 class="title" style="clear: both"><a name="keywordappendix"></a>B. Code for Keyword Example</h2><p> + <pre class="programlisting"> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <libxml/xmlmemory.h> +#include <libxml/parser.h> + +void +parseStory (xmlDocPtr doc, xmlNodePtr cur) { + + cur = cur->xmlChildrenNode; + while (cur != NULL) { + if ((!xmlStrcmp(cur->name, (const xmlChar *)"keyword"))) { + printf("keyword: %s\n", xmlNodeListGetString(doc, cur->xmlChildrenNode, 1)); + } + cur = cur->next; + } + return; +} + +static void +parseDoc(char *docname) { + + xmlDocPtr doc; + xmlNodePtr cur; + + doc = xmlParseFile(docname); + + if (doc == NULL ) { + fprintf(stderr,"Document not parsed successfully. \n"); + return; + } + + cur = xmlDocGetRootElement(doc); + + if (cur == NULL) { + fprintf(stderr,"empty document\n"); + xmlFreeDoc(doc); + return; + } + + if (xmlStrcmp(cur->name, (const xmlChar *) "story")) { + fprintf(stderr,"document of the wrong type, root node != story"); + xmlFreeDoc(doc); + return; + } + + cur = cur->xmlChildrenNode; + while (cur != NULL) { + if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){ + parseStory (doc, cur); + } + + cur = cur->next; + } + +} + +int +main(int argc, char **argv) { + + char *docname; + + if (argc <= 1) { + printf("Usage: %s docname\n", argv[0]); + return(0); + } + + docname = argv[1]; + parseDoc (docname); + + return (1); +} + +</pre> + </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="apa.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="apc.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">A. Sample Document </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> C. Code for Add Keyword Example</td></tr></table></div></body></html> diff --git a/doc/tutorial/apc.html b/doc/tutorial/apc.html new file mode 100644 index 00000000..62ca6b28 --- /dev/null +++ b/doc/tutorial/apc.html @@ -0,0 +1,77 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> +<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>C. Code for Add Keyword Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="apb.html" title="B. Code for Keyword Example"><link rel="next" href="apd.html" title="D. Code for Add Attribute Example"></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">C. Code for Add Keyword Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apb.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="apd.html">Next</a></td></tr></table><hr></div><div class="appendix"><h2 class="title" style="clear: both"><a name="addkeywordappendix"></a>C. Code for Add Keyword Example</h2><p> + <pre class="programlisting"> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <libxml/xmlmemory.h> +#include <libxml/parser.h> + +void +parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword) { + + xmlNewTextChild (cur, NULL, "keyword", keyword); + return; +} + +xmlDocPtr +parseDoc(char *docname, char *keyword) { + + xmlDocPtr doc; + xmlNodePtr cur; + + doc = xmlParseFile(docname); + + if (doc == NULL ) { + fprintf(stderr,"Document not parsed successfully. \n"); + return (NULL); + } + + cur = xmlDocGetRootElement(doc); + + if (cur == NULL) { + fprintf(stderr,"empty document\n"); + xmlFreeDoc(doc); + return (NULL); + } + + if (xmlStrcmp(cur->name, (const xmlChar *) "story")) { + fprintf(stderr,"document of the wrong type, root node != story"); + xmlFreeDoc(doc); + return (NULL); + } + + cur = cur->xmlChildrenNode; + while (cur != NULL) { + if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){ + parseStory (doc, cur, keyword); + } + + cur = cur->next; + } + return(doc); +} + +int +main(int argc, char **argv) { + + char *docname; + char *keyword; + xmlDocPtr doc; + + if (argc <= 2) { + printf("Usage: %s docname, keyword\n", argv[0]); + return(0); + } + + docname = argv[1]; + keyword = argv[2]; + doc = parseDoc (docname, keyword); + if (doc != NULL) { + xmlSaveFormatFile (docname, doc, 0); + } + return (1); +} + +</pre> + </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="apb.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="apd.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">B. Code for Keyword Example </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> D. Code for Add Attribute Example</td></tr></table></div></body></html> diff --git a/doc/tutorial/apd.html b/doc/tutorial/apd.html new file mode 100644 index 00000000..66cddfe3 --- /dev/null +++ b/doc/tutorial/apd.html @@ -0,0 +1,67 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> +<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>D. Code for Add Attribute Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="apc.html" title="C. Code for Add Keyword Example"></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">D. Code for Add Attribute Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apc.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> </td></tr></table><hr></div><div class="appendix"><h2 class="title" style="clear: both"><a name="addattributeappendix"></a>D. Code for Add Attribute Example</h2><p> + <pre class="programlisting"> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <libxml/xmlmemory.h> +#include <libxml/parser.h> + + +xmlDocPtr +parseDoc(char *docname, char *uri) { + + xmlDocPtr doc; + xmlNodePtr cur; + xmlNodePtr newnode; + xmlAttrPtr newattr; + + doc = xmlParseFile(docname); + + if (doc == NULL ) { + fprintf(stderr,"Document not parsed successfully. \n"); + return (NULL); + } + + cur = xmlDocGetRootElement(doc); + + if (cur == NULL) { + fprintf(stderr,"empty document\n"); + xmlFreeDoc(doc); + return (NULL); + } + + if (xmlStrcmp(cur->name, (const xmlChar *) "story")) { + fprintf(stderr,"document of the wrong type, root node != story"); + xmlFreeDoc(doc); + return (NULL); + } + + newnode = xmlNewTextChild (cur, NULL, "reference", NULL); + newattr = xmlNewProp (newnode, "uri", uri); + return(doc); +} + +int +main(int argc, char **argv) { + + char *docname; + char *uri; + xmlDocPtr doc; + + if (argc <= 2) { + printf("Usage: %s docname, uri\n", argv[0]); + return(0); + } + + docname = argv[1]; + uri = argv[2]; + doc = parseDoc (docname, uri); + if (doc != NULL) { + xmlSaveFormatFile (docname, doc, 1); + } + return (1); +} + +</pre> + </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="apc.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"> </td></tr><tr><td width="40%" align="left" valign="top">C. Code for Add Keyword Example </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> </td></tr></table></div></body></html> diff --git a/doc/tutorial/ar01s02.html b/doc/tutorial/ar01s02.html new file mode 100644 index 00000000..ba390211 --- /dev/null +++ b/doc/tutorial/ar01s02.html @@ -0,0 +1,11 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> +<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>Data Types</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="index.html" title="Libxml Tutorial"><link rel="next" href="ar01s03.html" title="Parsing the file"></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">Data Types</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="index.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ar01s03.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="xmltutorialdatatypes"></a>Data Types</h2></div></div><p>Libxml declares a number of datatypes we + will encounter repeatedly, hiding the messy stuff so you do not have to deal + with it unless you have some specific need.</p><p> + <div class="variablelist"><dl><dt><span class="term"><a href="http://xmlsoft.org/html/libxml-tree.html#XMLCHAR" target="_top">xmlChar</a></span></dt><dd><p>A basic replacement for char, a byte in a UTF-8 encoded + string.</p></dd><dt><span class="term"> + <a href="http://xmlsoft.org/html/libxml-tree.html#XMLDOC" target="_top">xmlDoc</a></span></dt><dd><p>A structure containing the tree created by a parsed doc. <a href="http://xmlsoft.org/html/libxml-tree.html#XMLDOCPTR" target="_top">xmlDocPtr</a> + is a pointer to the structure.</p></dd><dt><span class="term"><a href="http://xmlsoft.org/html/libxml-tree.html#XMLNODEPTR" target="_top">xmlNodePtr</a> + and <a href="http://xmlsoft.org/html/libxml-tree.html#XMLNODE" target="_top">xmlNode</a></span></dt><dd><p>A structure containing a single node. <a href="http://xmlsoft.org/html/libxml-tree.html#XMLNODEPTR" target="_top">xmlNodePtr</a> + is a pointer to the structure, and is used in traversing the document tree.</p></dd></dl></div> + </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="index.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="ar01s03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Libxml Tutorial </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Parsing the file</td></tr></table></div></body></html> diff --git a/doc/tutorial/ar01s03.html b/doc/tutorial/ar01s03.html new file mode 100644 index 00000000..62429fa1 --- /dev/null +++ b/doc/tutorial/ar01s03.html @@ -0,0 +1,33 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> +<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>Parsing the file</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="ar01s02.html" title="Data Types"><link rel="next" href="ar01s04.html" title="Retrieving 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">Parsing the file</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s02.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ar01s04.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="xmltutorialparsing"></a>Parsing the file</h2></div></div><p>Parsing the file requires only the name of the file and a single + function call, plus error checking. Full code: <a href="apb.html" title="B. Code for Keyword Example">Appendix B</a></p><p> + <pre class="programlisting"> + <a name="declaredoc"></a><img src="images/callouts/1.png" alt="1" border="0"> xmlDocPtr doc; + <a name="declarenode"></a><img src="images/callouts/2.png" alt="2" border="0"> xmlNodePtr cur; + + <a name="parsefile"></a><img src="images/callouts/3.png" alt="3" border="0"> doc = xmlParseFile(docname); + + <a name="checkparseerror"></a><img src="images/callouts/4.png" alt="4" border="0"> if (doc == NULL ) { + fprintf(stderr,"Document not parsed successfully. \n"); + return; + } + + <a name="getrootelement"></a><img src="images/callouts/5.png" alt="5" border="0"> cur = xmlDocGetRootElement(doc); + + <a name="checkemptyerror"></a><img src="images/callouts/6.png" alt="6" border="0"> if (cur == NULL) { + fprintf(stderr,"empty document\n"); + xmlFreeDoc(doc); + return; + } + + <a name="checkroottype"></a><img src="images/callouts/7.png" alt="7" border="0"> if (xmlStrcmp(cur->name, (const xmlChar *) "story")) { + fprintf(stderr,"document of the wrong type, root node != story"); + xmlFreeDoc(doc); + return; + } + + </pre> + <div class="calloutlist"><table border="0" summary="Callout list"><tr><td width="5%" valign="top" align="left"><a href="#declaredoc"><img src="images/callouts/1.png" alt="1" border="0"></a> </td><td valign="top" align="left"><p>Declare the pointer that will point to your parsed document.</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#declarenode"><img src="images/callouts/2.png" alt="2" border="0"></a> </td><td valign="top" align="left"><p>Declare a node pointer (you'll need this in order to + interact with individual nodes).</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#checkparseerror"><img src="images/callouts/4.png" alt="4" border="0"></a> </td><td valign="top" align="left"><p>Check to see that the document was successfully parsed.</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#getrootelement"><img src="images/callouts/5.png" alt="5" border="0"></a> </td><td valign="top" align="left"><p>Retrieve the document's root element.</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#checkemptyerror"><img src="images/callouts/6.png" alt="6" border="0"></a> </td><td valign="top" align="left"><p>Check to make sure the document actually contains something.</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#checkroottype"><img src="images/callouts/7.png" alt="7" border="0"></a> </td><td valign="top" align="left"><p>In our case, we need to make sure the document is the right + type. "story" is the root type of my documents.</p></td></tr></table></div> + </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s02.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="ar01s04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Data Types </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Retrieving Element Content</td></tr></table></div></body></html> diff --git a/doc/tutorial/ar01s04.html b/doc/tutorial/ar01s04.html new file mode 100644 index 00000000..8a9867d0 --- /dev/null +++ b/doc/tutorial/ar01s04.html @@ -0,0 +1,50 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> +<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>Retrieving Element Content</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><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="Writing 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"><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="xmltutorialgettext"></a>Retrieving Element Content</h2></div></div><p>Retrieving the content of an element involves traversing the document + tree until you find what you are looking for. In this case, we are looking + for an element called "keyword" contained within element called "story". The + process to find the node we are interested in involves tediously walking the + tree. We assume you already have an xmlDocPtr called <tt>doc</tt> + and an xmlNodPtr called <tt>cur</tt>.</p><p> + <pre class="programlisting"> + <a name="getchildnode"></a><img src="images/callouts/1.png" alt="1" border="0"> cur = cur->xmlChildrenNode; + <a name="huntstoryinfo"></a><img src="images/callouts/2.png" alt="2" border="0"> while (cur != NULL) { + if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){ + parseStory (doc, cur); + } + + cur = cur->next; + } + + </pre> + + <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>cur</tt>. At this + point, <tt>cur</tt> points at the document root, which is + 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 + "story", looking for one called "storyinfo". That + is the element that will contain the "keywords" we are + looking for. It uses the libxml string + comparison + function, <tt><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>parseStory</tt>.</p></td></tr></table></div> + </p><p> + <pre class="programlisting"> +void +parseStory (xmlDocPtr doc, xmlNodePtr cur) { + + <a name="anothergetchild"></a><img src="images/callouts/1.png" alt="1" border="0"> cur = cur->xmlChildrenNode; + <a name="findkeyword"></a><img src="images/callouts/2.png" alt="2" border="0"> while (cur != NULL) { + if ((!xmlStrcmp(cur->name, (const xmlChar *)"keyword"))) { + <a name="foundkeyword"></a><img src="images/callouts/3.png" alt="3" border="0"> printf("keyword: %s\n", xmlNodeListGetString(doc, cur->xmlChildrenNode, 1)); + } + cur = cur->next; + } + return; +} + </pre> + <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 + for one that matches the element we're interested in, in this case + "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 + its contents. Remember that in XML, the text + contained within an element is a child node of that element, so we + turn to <tt>cur->xmlChildrenNode</tt>. To retrieve it, we + use the function <tt><a href="http://xmlsoft.org/html/libxml-tree.html#XMLNODELISTGETSTRING" target="_top">xmlNodeListGetString</a></tt>, which also takes the <tt>doc</tt> pointer as an argument. In this case, we just print it out.</p></td></tr></table></div> + </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"> Writing element content</td></tr></table></div></body></html> diff --git a/doc/tutorial/ar01s05.html b/doc/tutorial/ar01s05.html new file mode 100644 index 00000000..5f90af87 --- /dev/null +++ b/doc/tutorial/ar01s05.html @@ -0,0 +1,34 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> +<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>Writing element content</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="ar01s04.html" title="Retrieving Element Content"><link rel="next" href="ar01s06.html" title="Writing Attribute"></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">Writing element content</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s04.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ar01s06.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="xmltutorialwritingcontent"></a>Writing element content</h2></div></div><p>Writing element content uses many of the same steps we used above + — parsing the document and walking the tree. We parse the document, + then traverse the tree to find the place we want to insert our element. For + this example, we want to again find the "storyinfo" element and + this time insert a keyword. Then we'll write the file to disk. Full code: + <a href="apc.html" title="C. Code for Add Keyword Example">Appendix C</a></p><p> + The main difference in this example is in + <tt>parseStory</tt>: + + <pre class="programlisting"> +void +parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword) { + + <a name="addkeyword"></a><img src="images/callouts/1.png" alt="1" border="0"> xmlNewTextChild (cur, NULL, "keyword", keyword); + return; +} + </pre> + <div class="calloutlist"><table border="0" summary="Callout list"><tr><td width="5%" valign="top" align="left"><a href="#addkeyword"><img src="images/callouts/1.png" alt="1" border="0"></a> </td><td valign="top" align="left"><p>The <tt><a href="http://xmlsoft.org/html/libxml-tree.html#XMLNEWTEXTCHILD" target="_top">xmlNewTextChild</a></tt> + function adds a new child element at the + current node pointer's location in the + tree, specificied by <tt>cur</tt>.</p></td></tr></table></div> + </p><p> + Once the node has been added, we would like to write the document to + file. Is you want the element to have a namespace, you can add it here as + well. In our case, the namespace is NULL. + <pre class="programlisting"> + xmlSaveFormatFile (docname, doc, 1); + </pre> + The first parameter is the name of the file to be written. You'll notice + it is the same as the file we just read. In this case, we just write over + the old file. The second parameter is a pointer to the xmlDoc + structure. Setting the third parameter equal to one ensures indenting on output. + </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s04.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="ar01s06.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Retrieving Element Content </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Writing Attribute</td></tr></table></div></body></html> diff --git a/doc/tutorial/ar01s06.html b/doc/tutorial/ar01s06.html new file mode 100644 index 00000000..8d2afd15 --- /dev/null +++ b/doc/tutorial/ar01s06.html @@ -0,0 +1,30 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> +<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>Writing Attribute</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="ar01s05.html" title="Writing element content"><link rel="next" href="apa.html" title="A. Sample Document"></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">Writing Attribute</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s05.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="apa.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="xmltutorialwritingattribute"></a>Writing Attribute</h2></div></div><p>Writing an attribute is similar to writing text to a new element. In + this case, we'll add a reference URI to our + document. Full code:<a href="apd.html" title="D. Code for Add Attribute Example">Appendix D</a>.</p><p> + A <tt class="sgmltag-element">reference</tt> is a child of the <tt class="sgmltag-element">story</tt> + element, so finding the place to put our new element and attribute is + simple. As soon as we do the error-checking test in our + <tt>parseDoc</tt>, we are in the right spot to add our + element. But before we do that, we need to make a declaration using a + datatype we have not seen yet: + <pre class="programlisting"> + xmlAttrPtr newattr; + </pre> + We also need an extra xmlNodePtr: + <pre class="programlisting"> + xmlNodePtr newnode; + </pre> + </p><p> + The rest of <tt>parseDoc</tt> is the same as before until we + check to see if our root element is <tt class="sgmltag-element">story</tt>. If it is, + then we know we are at the right spot to add our element: + + <pre class="programlisting"> + <a name="addreferencenode"></a><img src="images/callouts/1.png" alt="1" border="0"> newnode = xmlNewTextChild (cur, NULL, "reference", NULL); + <a name="addattributenode"></a><img src="images/callouts/2.png" alt="2" border="0"> newattr = xmlNewProp (newnode, "uri", uri); + </pre> + <div class="calloutlist"><table border="0" summary="Callout list"><tr><td width="5%" valign="top" align="left"><a href="#addreferencenode"><img src="images/callouts/1.png" alt="1" border="0"></a> </td><td valign="top" align="left"><p>First we add a new node at the location of the current node + pointer, <tt>cur.</tt> using the <a href="http://xmlsoft.org/html/libxml-tree.html#XMLNEWTEXTCHILD" target="_top">xmlNewTextChild</a> function.</p></td></tr></table></div> + </p><p>Once the node is added, the file is written to disk just as in the + previous example in which we added an element with text content.</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s05.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="apa.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Writing element content </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> A. Sample Document</td></tr></table></div></body></html> diff --git a/doc/tutorial/includeaddattribute.c b/doc/tutorial/includeaddattribute.c new file mode 100644 index 00000000..45321b3f --- /dev/null +++ b/doc/tutorial/includeaddattribute.c @@ -0,0 +1,63 @@ +<![CDATA[ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <libxml/xmlmemory.h> +#include <libxml/parser.h> + + +xmlDocPtr +parseDoc(char *docname, char *uri) { + + xmlDocPtr doc; + xmlNodePtr cur; + xmlNodePtr newnode; + xmlAttrPtr newattr; + + doc = xmlParseFile(docname); + + if (doc == NULL ) { + fprintf(stderr,"Document not parsed successfully. \n"); + return (NULL); + } + + cur = xmlDocGetRootElement(doc); + + if (cur == NULL) { + fprintf(stderr,"empty document\n"); + xmlFreeDoc(doc); + return (NULL); + } + + if (xmlStrcmp(cur->name, (const xmlChar *) "story")) { + fprintf(stderr,"document of the wrong type, root node != story"); + xmlFreeDoc(doc); + return (NULL); + } + + newnode = xmlNewTextChild (cur, NULL, "reference", NULL); + newattr = xmlNewProp (newnode, "uri", uri); + return(doc); +} + +int +main(int argc, char **argv) { + + char *docname; + char *uri; + xmlDocPtr doc; + + if (argc <= 2) { + printf("Usage: %s docname, uri\n", argv[0]); + return(0); + } + + docname = argv[1]; + uri = argv[2]; + doc = parseDoc (docname, uri); + if (doc != NULL) { + xmlSaveFormatFile (docname, doc, 1); + } + return (1); +} +]]> diff --git a/doc/tutorial/includeaddkeyword.c b/doc/tutorial/includeaddkeyword.c new file mode 100644 index 00000000..8f5de904 --- /dev/null +++ b/doc/tutorial/includeaddkeyword.c @@ -0,0 +1,73 @@ +<![CDATA[ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <libxml/xmlmemory.h> +#include <libxml/parser.h> + +void +parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword) { + + xmlNewTextChild (cur, NULL, "keyword", keyword); + return; +} + +xmlDocPtr +parseDoc(char *docname, char *keyword) { + + xmlDocPtr doc; + xmlNodePtr cur; + + doc = xmlParseFile(docname); + + if (doc == NULL ) { + fprintf(stderr,"Document not parsed successfully. \n"); + return (NULL); + } + + cur = xmlDocGetRootElement(doc); + + if (cur == NULL) { + fprintf(stderr,"empty document\n"); + xmlFreeDoc(doc); + return (NULL); + } + + if (xmlStrcmp(cur->name, (const xmlChar *) "story")) { + fprintf(stderr,"document of the wrong type, root node != story"); + xmlFreeDoc(doc); + return (NULL); + } + + cur = cur->xmlChildrenNode; + while (cur != NULL) { + if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){ + parseStory (doc, cur, keyword); + } + + cur = cur->next; + } + return(doc); +} + +int +main(int argc, char **argv) { + + char *docname; + char *keyword; + xmlDocPtr doc; + + if (argc <= 2) { + printf("Usage: %s docname, keyword\n", argv[0]); + return(0); + } + + docname = argv[1]; + keyword = argv[2]; + doc = parseDoc (docname, keyword); + if (doc != NULL) { + xmlSaveFormatFile (docname, doc, 0); + } + return (1); +} +]]> diff --git a/doc/tutorial/includekeyword.c b/doc/tutorial/includekeyword.c new file mode 100644 index 00000000..af2627d4 --- /dev/null +++ b/doc/tutorial/includekeyword.c @@ -0,0 +1,74 @@ +<![CDATA[ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <libxml/xmlmemory.h> +#include <libxml/parser.h> + +void +parseStory (xmlDocPtr doc, xmlNodePtr cur) { + + cur = cur->xmlChildrenNode; + while (cur != NULL) { + if ((!xmlStrcmp(cur->name, (const xmlChar *)"keyword"))) { + printf("keyword: %s\n", xmlNodeListGetString(doc, cur->xmlChildrenNode, 1)); + } + cur = cur->next; + } + return; +} + +static void +parseDoc(char *docname) { + + xmlDocPtr doc; + xmlNodePtr cur; + + doc = xmlParseFile(docname); + + if (doc == NULL ) { + fprintf(stderr,"Document not parsed successfully. \n"); + return; + } + + cur = xmlDocGetRootElement(doc); + + if (cur == NULL) { + fprintf(stderr,"empty document\n"); + xmlFreeDoc(doc); + return; + } + + if (xmlStrcmp(cur->name, (const xmlChar *) "story")) { + fprintf(stderr,"document of the wrong type, root node != story"); + xmlFreeDoc(doc); + return; + } + + cur = cur->xmlChildrenNode; + while (cur != NULL) { + if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){ + parseStory (doc, cur); + } + + cur = cur->next; + } + +} + +int +main(int argc, char **argv) { + + char *docname; + + if (argc <= 1) { + printf("Usage: %s docname\n", argv[0]); + return(0); + } + + docname = argv[1]; + parseDoc (docname); + + return (1); +} +]]> diff --git a/doc/tutorial/includestory.xml b/doc/tutorial/includestory.xml new file mode 100644 index 00000000..2a22c899 --- /dev/null +++ b/doc/tutorial/includestory.xml @@ -0,0 +1,14 @@ +<![CDATA[ +<?xml version="1.0"?> +<story> + <storyinfo> + <author>John Fleck</author> + <datewritten>June 2, 2002</datewritten> + <keyword>example keyword</keyword> + </storyinfo> + <body> + <headline>This is the headline</headline> + <para>This is the body text.</para> + </body> +</story> +]]>
\ No newline at end of file diff --git a/doc/tutorial/index.html b/doc/tutorial/index.html new file mode 100644 index 00000000..eef00e37 --- /dev/null +++ b/doc/tutorial/index.html @@ -0,0 +1,16 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> +<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>Libxml Tutorial</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="next" href="ar01s02.html" title="Data Types"></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">Libxml Tutorial</th></tr><tr><td width="20%" align="left"> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ar01s02.html">Next</a></td></tr></table><hr></div><div class="article"><div class="titlepage"><div><h1 class="title"><a name="id2759085"></a>Libxml Tutorial</h1></div><div><h3 class="author">John Fleck</h3></div><div><p class="copyright">Copyright © 2002 John Fleck</p></div><div><div class="revhistory"><table border="1" width="100%" summary="Revision history"><tr><th align="left" valign="top" colspan="2"><b>Revision History</b></th></tr><tr><td align="left">Revision 1</td><td align="left">June 4,2002</td></tr></table></div></div><hr></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><a href="index.html#introduction">Introduction</a></dt><dt><a href="ar01s02.html">Data Types</a></dt><dt><a href="ar01s03.html">Parsing the file</a></dt><dt><a href="ar01s04.html">Retrieving Element Content</a></dt><dt><a href="ar01s05.html">Writing element content</a></dt><dt><a href="ar01s06.html">Writing Attribute</a></dt><dt>A. <a href="apa.html">Sample Document</a></dt><dt>B. <a href="apb.html">Code for Keyword Example</a></dt><dt>C. <a href="apc.html">Code for Add Keyword Example</a></dt><dt>D. <a href="apd.html">Code for Add Attribute Example</a></dt></dl></div><div class="abstract"><p><b>Abstract</b></p><p>Libxml is a freely licensed C language library for handling + XML, portable across a large number of platforms. This + tutorial provides examples of its basic functions.</p></div><div class="sect1"><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="introduction"></a>Introduction</h2></div></div><p>Libxml is a C language library implementing functions for reading, + creating and manipulating XML data. This tutorial + provides example code and explanations of its basic functionality.</p><p>Libxml and more details about its use are available on <a href="http://www.xmlsoft.org/" target="_top">the project home page</a>. Included there is complete <a href="http://xmlsoft.org/html/libxml-lib.html" target="_top"> + API documentation</a>. This tutorial is not meant + to substitute for that complete documentation, but to illustrate the + functions needed to use the library to perform basic operations. + +</p><p>The tutorial is based on a simple XML application I + use for articles I write. The format includes metadata and the body + of the article.</p><p>The example code in this tutorial demonstrates how to: + <div class="itemizedlist"><ul type="disc"><li><p>Parse the document.</p></li><li><p>Extract the text within a specified element.</p></li><li><p>Add an element and its content.</p></li><li><p>Extract the value of an attribute.</p></li><li><p>Add an attribute.</p></li></ul></div> + + </p><p>Full code for the examples is included in the appendices.</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ar01s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top"> </td><td width="20%" align="center"> </td><td width="40%" align="right" valign="top"> Data Types</td></tr></table></div></body></html> diff --git a/doc/tutorial/xmltutorial.xml b/doc/tutorial/xmltutorial.xml new file mode 100644 index 00000000..be9a1ba3 --- /dev/null +++ b/doc/tutorial/xmltutorial.xml @@ -0,0 +1,372 @@ +<?xml version="1.0"?> +<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" + "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [ +<!ENTITY KEYWORD SYSTEM "includekeyword.c"> +<!ENTITY STORY SYSTEM "includestory.xml"> +<!ENTITY ADDKEYWORD SYSTEM "includeaddkeyword.c"> +<!ENTITY ADDATTRIBUTE SYSTEM "includeaddattribute.c"> +]> +<article> + <articleinfo> + <title>Libxml Tutorial</title> + <author> + <firstname>John</firstname> + <surname>Fleck</surname> + </author> + <copyright> + <year>2002</year> + <holder>John Fleck</holder> + </copyright> + <revhistory> + <revision> + <revnumber>1</revnumber> + <date>June 4,2002</date> + </revision> + </revhistory> + </articleinfo> + <abstract> + <para>Libxml is a freely licensed C language library for handling + <acronym>XML</acronym>, portable across a large number of platforms. This + tutorial provides examples of its basic functions.</para> + </abstract> + <sect1 id="introduction"> + <title>Introduction</title> + <para>Libxml is a C language library implementing functions for reading, + creating and manipulating <acronym>XML</acronym> data. This tutorial + provides example code and explanations of its basic functionality.</para> + <para>Libxml and more details about its use are available on <ulink + url="http://www.xmlsoft.org/">the project home page</ulink>. Included there is complete <ulink url="http://xmlsoft.org/html/libxml-lib.html"> + <acronym>API</acronym> documentation</ulink>. This tutorial is not meant + to substitute for that complete documentation, but to illustrate the + functions needed to use the library to perform basic operations. +<!-- + Links to + other resources can be found in <xref linkend="furtherresources" />. +--> +</para> + <para>The tutorial is based on a simple <acronym>XML</acronym> application I + use for articles I write. The format includes metadata and the body + of the article.</para> + <para>The example code in this tutorial demonstrates how to: + <itemizedlist> + <listitem> + <para>Parse the document.</para> + </listitem> + <listitem> + <para>Extract the text within a specified element.</para> + </listitem> + <listitem> + <para>Add an element and its content.</para> + </listitem> + <listitem> + <para>Extract the value of an attribute.</para> + </listitem> + <listitem> + <para>Add an attribute.</para> + </listitem> + </itemizedlist> + + </para> + <para>Full code for the examples is included in the appendices.</para> + + </sect1> + + <sect1 id="xmltutorialdatatypes"> + <title>Data Types</title> + <para><application>Libxml</application> declares a number of datatypes we + will encounter repeatedly, hiding the messy stuff so you do not have to deal + with it unless you have some specific need.</para> + <para> + <variablelist> + <varlistentry> + <term><ulink + url="http://xmlsoft.org/html/libxml-tree.html#XMLCHAR">xmlChar</ulink></term> + <listitem> + <para>A basic replacement for char, a byte in a UTF-8 encoded + string.</para> + </listitem> + </varlistentry> + <varlistentry> + <term> + <ulink url="http://xmlsoft.org/html/libxml-tree.html#XMLDOC">xmlDoc</ulink></term> + <listitem> + <para>A structure containing the tree created by a parsed doc. <ulink + url="http://xmlsoft.org/html/libxml-tree.html#XMLDOCPTR">xmlDocPtr</ulink> + is a pointer to the structure.</para> + </listitem> + </varlistentry> + <varlistentry> + <term><ulink + url="http://xmlsoft.org/html/libxml-tree.html#XMLNODEPTR">xmlNodePtr</ulink> + and <ulink url="http://xmlsoft.org/html/libxml-tree.html#XMLNODE">xmlNode</ulink></term> + <listitem> + <para>A structure containing a single node. <ulink + url="http://xmlsoft.org/html/libxml-tree.html#XMLNODEPTR">xmlNodePtr</ulink> + is a pointer to the structure, and is used in traversing the document tree.</para> + </listitem> + </varlistentry> + </variablelist> + </para> + + </sect1> + + <sect1 id="xmltutorialparsing"> + <title>Parsing the file</title> + <para>Parsing the file requires only the name of the file and a single + function call, plus error checking. Full code: <xref + linkend="keywordappendix" /></para> + <para> + <programlisting> + <co id="declaredoc" /> xmlDocPtr doc; + <co id="declarenode" /> xmlNodePtr cur; + + <co id="parsefile" /> doc = xmlParseFile(docname); + + <co id="checkparseerror" /> if (doc == NULL ) { + fprintf(stderr,"Document not parsed successfully. \n"); + return; + } + + <co id="getrootelement" /> cur = xmlDocGetRootElement(doc); + + <co id="checkemptyerror" /> if (cur == NULL) { + fprintf(stderr,"empty document\n"); + xmlFreeDoc(doc); + return; + } + + <co id="checkroottype" /> if (xmlStrcmp(cur->name, (const xmlChar *) "story")) { + fprintf(stderr,"document of the wrong type, root node != story"); + xmlFreeDoc(doc); + return; + } + + </programlisting> + <calloutlist> + <callout arearefs="declaredoc"> + <para>Declare the pointer that will point to your parsed document.</para> + </callout> + <callout arearefs="declarenode"> + <para>Declare a node pointer (you'll need this in order to + interact with individual nodes).</para> + </callout> + <callout arearefs="checkparseerror"> + <para>Check to see that the document was successfully parsed.</para> + </callout> + <callout arearefs="getrootelement"> + <para>Retrieve the document's root element.</para> + </callout> + <callout arearefs="checkemptyerror"> + <para>Check to make sure the document actually contains something.</para> + </callout> + <callout arearefs="checkroottype"> + <para>In our case, we need to make sure the document is the right + type. "story" is the root type of my documents.</para> + </callout> + </calloutlist> + </para> + </sect1> + + <sect1 id="xmltutorialgettext"> + <title>Retrieving Element Content</title> + <para>Retrieving the content of an element involves traversing the document + tree until you find what you are looking for. In this case, we are looking + for an element called "keyword" contained within element called "story". The + process to find the node we are interested in involves tediously walking the + tree. We assume you already have an xmlDocPtr called <varname>doc</varname> + and an xmlNodPtr called <varname>cur</varname>.</para> + + <para> + <programlisting> + <co id="getchildnode" /> cur = cur->xmlChildrenNode; + <co id="huntstoryinfo" /> while (cur != NULL) { + if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){ + parseStory (doc, cur); + } + + cur = cur->next; + } + + </programlisting> + + <calloutlist> + <callout arearefs="getchildnode"> + <para>Get the first child node of <varname>cur</varname>. At this + point, <varname>cur</varname> points at the document root, which is + the element "story".</para> + </callout> + <callout arearefs="huntstoryinfo"> + <para>This loop iterates through the elements that are children of + "story", looking for one called "storyinfo". That + is the element that will contain the "keywords" we are + looking for. It uses the <application>libxml</application> string + comparison + function, <function><ulink + url="http://xmlsoft.org/html/libxml-parser.html#XMLSTRCMP">xmlStrcmp</ulink></function>. If there is a match, it calls the function <function>parseStory</function>.</para> + </callout> + </calloutlist> + </para> + + <para> + <programlisting> +void +parseStory (xmlDocPtr doc, xmlNodePtr cur) { + + <co id="anothergetchild" /> cur = cur->xmlChildrenNode; + <co id="findkeyword" /> while (cur != NULL) { + if ((!xmlStrcmp(cur->name, (const xmlChar *)"keyword"))) { + <co id="foundkeyword" /> printf("keyword: %s\n", xmlNodeListGetString(doc, cur->xmlChildrenNode, 1)); + } + cur = cur->next; + } + return; +} + </programlisting> + <calloutlist> + <callout arearefs="anothergetchild"> + <para>Again we get the first child node.</para> + </callout> + <callout arearefs="findkeyword"> + <para>Like the loop above, we then iterate through the nodes, looking + for one that matches the element we're interested in, in this case + "keyword".</para> + </callout> + <callout arearefs="foundkeyword"> + <para>When we find the "keyword" element, we need to print + its contents. Remember that in <acronym>XML</acronym>, the text + contained within an element is a child node of that element, so we + turn to <varname>cur->xmlChildrenNode</varname>. To retrieve it, we + use the function <function><ulink + url="http://xmlsoft.org/html/libxml-tree.html#XMLNODELISTGETSTRING">xmlNodeListGetString</ulink></function>, which also takes the <varname>doc</varname> pointer as an argument. In this case, we just print it out.</para> + </callout> + </calloutlist> + </para> + + </sect1> + +<sect1 id="xmltutorialwritingcontent"> + <title>Writing element content</title> + <para>Writing element content uses many of the same steps we used above + — parsing the document and walking the tree. We parse the document, + then traverse the tree to find the place we want to insert our element. For + this example, we want to again find the "storyinfo" element and + this time insert a keyword. Then we'll write the file to disk. Full code: + <xref linkend="addkeywordappendix" /></para> + + <para> + The main difference in this example is in + <function>parseStory</function>: + + <programlisting> +void +parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword) { + + <co id="addkeyword" /> xmlNewTextChild (cur, NULL, "keyword", keyword); + return; +} + </programlisting> + <calloutlist> + <callout arearefs="addkeyword"> + <para>The <function><ulink + url="http://xmlsoft.org/html/libxml-tree.html#XMLNEWTEXTCHILD">xmlNewTextChild</ulink></function> + function adds a new child element at the + current node pointer's location in the + tree, specificied by <varname>cur</varname>.</para> + </callout> + </calloutlist> + </para> + + <para> + Once the node has been added, we would like to write the document to + file. Is you want the element to have a namespace, you can add it here as + well. In our case, the namespace is NULL. + <programlisting> + xmlSaveFormatFile (docname, doc, 1); + </programlisting> + The first parameter is the name of the file to be written. You'll notice + it is the same as the file we just read. In this case, we just write over + the old file. The second parameter is a pointer to the xmlDoc + structure. Setting the third parameter equal to one ensures indenting on output. + </para> + + </sect1> +<!-- + <sect1 id="xmltutorialattribute"> + <title>Retrieving Attributes</title> + <para>Retrieving</para> + </sect1> +--> + + + <sect1 id="xmltutorialwritingattribute"> + <title>Writing Attribute</title> + <para>Writing an attribute is similar to writing text to a new element. In + this case, we'll add a reference <acronym>URI</acronym> to our + document. Full code:<xref linkend="addattributeappendix" />.</para> + <para> + A <sgmltag>reference</sgmltag> is a child of the <sgmltag>story</sgmltag> + element, so finding the place to put our new element and attribute is + simple. As soon as we do the error-checking test in our + <function>parseDoc</function>, we are in the right spot to add our + element. But before we do that, we need to make a declaration using a + datatype we have not seen yet: + <programlisting> + xmlAttrPtr newattr; + </programlisting> + We also need an extra xmlNodePtr: + <programlisting> + xmlNodePtr newnode; + </programlisting> + </para> + <para> + The rest of <function>parseDoc</function> is the same as before until we + check to see if our root element is <sgmltag>story</sgmltag>. If it is, + then we know we are at the right spot to add our element: + + <programlisting> + <co id="addreferencenode" /> newnode = xmlNewTextChild (cur, NULL, "reference", NULL); + <co id="addattributenode" /> newattr = xmlNewProp (newnode, "uri", uri); + </programlisting> + <calloutlist> + <callout arearefs="addreferencenode"> + <para>First we add a new node at the location of the current node + pointer, <varname>cur.</varname> using the <ulink + url="http://xmlsoft.org/html/libxml-tree.html#XMLNEWTEXTCHILD">xmlNewTextChild</ulink> function.</para> + </callout> + </calloutlist> + </para> + + <para>Once the node is added, the file is written to disk just as in the + previous example in which we added an element with text content.</para> + + </sect1> + +<!-- + <appendix id="furtherresources"> + <title>Further Resources</title> + <para></para> + </appendix> +--> + <appendix id="sampledoc"> + <title>Sample Document</title> + <programlisting>&STORY;</programlisting> + </appendix> + <appendix id="keywordappendix"> + <title>Code for Keyword Example</title> + <para> + <programlisting>&KEYWORD;</programlisting> + </para> + </appendix> +<appendix id="addkeywordappendix"> + <title>Code for Add Keyword Example</title> + <para> + <programlisting>&ADDKEYWORD;</programlisting> + </para> + </appendix> +<appendix id="addattributeappendix"> + <title>Code for Add Attribute Example</title> + <para> + <programlisting>&ADDATTRIBUTE;</programlisting> + </para> + </appendix> +</article> |
