Commit bd3b4fd1 authored by MST 2002 John Fleck's avatar MST 2002 John Fleck Committed by John Fleck
Browse files

Adding encoding discussion to tutorial Added: DocBook admonition image


Sun Nov 10 20:33:13 MST 2002 John Fleck  <jfleck@inkstain.net>

	Adding encoding discussion to tutorial
	Added:
	* doc/tutorial/images/*.png: DocBook admonition image files
	* doc/tutorial/apf.html, apg.html: new generated html
	* doc/tutorial/includeconvert.c: conversion code entity file
	changed:
	* doc/tutorial/xmltutorial.xml: DocBook original
	* doc/tutorial/*.html: generated html
parent 22baffff
Sun Nov 10 20:33:13 MST 2002 John Fleck <jfleck@inkstain.net>
Adding encoding discussion to tutorial
Added:
* doc/tutorial/images/*.png: DocBook admonition image files
* doc/tutorial/apf.html, apg.html: new generated html
* doc/tutorial/includeconvert.c: conversion code entity file
changed:
* doc/tutorial/xmltutorial.xml: DocBook original
* doc/tutorial/*.html: generated html
Fri Nov 8 17:59:32 CEST 2002 Igor Zlatkovic <igor@stud.fh-frankfurt.de>
* include/libxml/*.h: retired xmlwin32version.h
......
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><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="ar01s07.html" title="Retrieving Attributes"><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="ar01s07.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">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>A. Sample Document</title><meta name="generator" content="DocBook XSL Stylesheets V1.57.0"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="ar01s08.html" title="Encoding Conversion"><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="ar01s08.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" lang="en"><div class="titlepage"><div><h2 class="title"><a name="sampledoc"></a>A. Sample Document</h2></div></div><pre class="programlisting">
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;story&gt;
&lt;storyinfo&gt;
......@@ -12,4 +12,4 @@
&lt;para&gt;This is the body text.&lt;/para&gt;
&lt;/body&gt;
&lt;/story&gt;
</pre></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s07.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">Retrieving Attributes</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>
</pre></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s08.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">Encoding Conversion</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>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><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">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>B. Code for Keyword Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.57.0"><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" lang="en"><div class="titlepage"><div><h2 class="title"><a name="keywordappendix"></a>B. Code for Keyword Example</h2></div></div><p>
</p><pre class="programlisting">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
......@@ -77,5 +77,5 @@ main(int argc, char **argv) {
return (1);
}
</pre>
</pre><p>
</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>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><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">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>C. Code for Add Keyword Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.57.0"><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" lang="en"><div class="titlepage"><div><h2 class="title"><a name="addkeywordappendix"></a>C. Code for Add Keyword Example</h2></div></div><p>
</p><pre class="programlisting">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
......@@ -76,5 +76,5 @@ main(int argc, char **argv) {
return (1);
}
</pre>
</pre><p>
</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>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><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"><link rel="next" href="ape.html" title="E. Code for Retrieving Attribute Value 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"><a accesskey="n" href="ape.html">Next</a></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">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>D. Code for Add Attribute Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.57.0"><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"><link rel="next" href="ape.html" title="E. Code for Retrieving Attribute Value 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"><a accesskey="n" href="ape.html">Next</a></td></tr></table><hr></div><div class="appendix" lang="en"><div class="titlepage"><div><h2 class="title"><a name="addattributeappendix"></a>D. Code for Add Attribute Example</h2></div></div><p>
</p><pre class="programlisting">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
......@@ -64,5 +64,5 @@ main(int argc, char **argv) {
return (1);
}
</pre>
</pre><p>
</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"><a accesskey="n" href="ape.html">Next</a></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">E. Code for Retrieving Attribute Value Example</td></tr></table></div></body></html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>E. Code for Retrieving Attribute Value 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="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">E. Code for Retrieving Attribute Value Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apd.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="getattributeappendix"></a>E. Code for Retrieving Attribute Value Example</h2><p>
<pre class="programlisting">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>E. Code for Retrieving Attribute Value Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.57.0"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="apd.html" title="D. Code for Add Attribute Example"><link rel="next" href="apf.html" title="F. Code for Encoding Conversion 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">E. Code for Retrieving Attribute Value Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apd.html">Prev</a></td><th width="60%" align="center"></th><td width="20%" align="right"><a accesskey="n" href="apf.html">Next</a></td></tr></table><hr></div><div class="appendix" lang="en"><div class="titlepage"><div><h2 class="title"><a name="getattributeappendix"></a>E. Code for Retrieving Attribute Value Example</h2></div></div><p>
</p><pre class="programlisting">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
......@@ -69,5 +69,5 @@ main(int argc, char **argv) {
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="apd.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">D. Code for Add Attribute 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>
</pre><p>
</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="apd.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="apf.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">D. Code for Add Attribute Example</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">F. Code for Encoding Conversion Example</td></tr></table></div></body></html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>F. Code for Encoding Conversion Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.57.0"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="ape.html" title="E. Code for Retrieving Attribute Value Example"><link rel="next" href="apg.html" title="G. Acknowledgements"></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">F. Code for Encoding Conversion Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ape.html">Prev</a></td><th width="60%" align="center"></th><td width="20%" align="right"><a accesskey="n" href="apg.html">Next</a></td></tr></table><hr></div><div class="appendix" lang="en"><div class="titlepage"><div><h2 class="title"><a name="convertappendix"></a>F. Code for Encoding Conversion Example</h2></div></div><p>
</p><pre class="programlisting">
#include &lt;string.h&gt;
#include &lt;libxml/parser.h&gt;
unsigned char*
convert (unsigned char *in, char *encoding)
{
unsigned char *out;
int ret,size,out_size,temp;
xmlCharEncodingHandlerPtr handler;
size = (int)strlen(in)+1;
out_size = size*2-1;
out = malloc((size_t)out_size);
if (out) {
handler = xmlFindCharEncodingHandler(encoding);
if (!handler) {
free(out);
out = NULL;
}
}
if (out) {
temp=size-1;
ret = handler-&gt;input(out, &amp;out_size, in, &amp;temp);
if (ret || temp-size+1) {
if (ret) {
printf(&quot;conversion wasn't successful.\n&quot;);
} else {
printf(&quot;conversion wasn't successful. converted: %i octets.\n&quot;,temp);
}
free(out);
out = NULL;
} else {
out = realloc(out,out_size+1);
out[out_size]=0; /*null terminating out*/
}
} else {
printf(&quot;no mem\n&quot;);
}
return (out);
}
int
main(int argc, char **argv) {
unsigned char *content, *out;
xmlDocPtr doc;
xmlNodePtr rootnode;
char *encoding = &quot;ISO-8859-1&quot;;
if (argc &lt;= 1) {
printf(&quot;Usage: %s content\n&quot;, argv[0]);
return(0);
}
content = argv[1];
out = convert(content, encoding);
doc = xmlNewDoc (&quot;1.0&quot;);
rootnode = xmlNewDocNode(doc, NULL, (const xmlChar*)&quot;root&quot;, out);
xmlDocSetRootElement(doc, rootnode);
xmlSaveFormatFileEnc(&quot;-&quot;, doc, encoding, 1);
return (1);
}
</pre><p>
</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ape.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="apg.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">E. Code for Retrieving Attribute Value Example</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">G. Acknowledgements</td></tr></table></div></body></html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>G. Acknowledgements</title><meta name="generator" content="DocBook XSL Stylesheets V1.57.0"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="apf.html" title="F. Code for Encoding Conversion 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">G. Acknowledgements</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apf.html">Prev</a></td><th width="60%" align="center"></th><td width="20%" align="right"></td></tr></table><hr></div><div class="appendix" lang="en"><div class="titlepage"><div><h2 class="title"><a name="id2802385"></a>G. Acknowledgements</h2></div></div><p>A number of people have generously offered feedback, code and
suggested improvements to this tutorial. In no particular order:
</p><table class="simplelist" border="0" summary="Simple list"><tr><td>Daniel Veillard</td></tr><tr><td>Marcus Labib Iskander</td></tr><tr><td>Christopher R. Harris</td></tr><tr><td>Igor Zlatkovic</td></tr></table><p>
</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="apf.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">F. Code for Encoding Conversion 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>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><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
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Data Types</title><meta name="generator" content="DocBook XSL Stylesheets V1.57.0"><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" lang="en"><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 data types 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">
</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. If your data uses another encoding, it must be converted to
UTF-8 for use with libxml's
functions. More information on encoding is available on the <a href="http://www.xmlsoft.org/encoding.html" target="_top">libxml encoding support web page</a>.</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>
is a pointer to the structure, and is used in traversing the document tree.</p></dd></dl></div><p>
</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>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><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">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Parsing the file</title><meta name="generator" content="DocBook XSL Stylesheets V1.57.0"><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" lang="en"><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">AppendixB</a></p><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;
......@@ -27,8 +27,20 @@
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. &quot;story&quot; is the root type of my documents.</p></td></tr></table></div>
</pre><p>
</p><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. If it
was not, libxml will at this point
register an error and stop.
</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>Note</th></tr><tr><td colspan="2" align="left" valign="top"><p>One common example of an error at this point is improper
handling of encoding. The XML standard requires
documents stored with an encoding other than UTF-8 or UTF-16 to
contain an explicit declaration of their encoding. If the
declaration is there, libxml will
automatically perform the necessary conversion to UTF-8 for
you. More information on XML's encoding
requirements is contained in the <a href="http://www.w3.org/TR/REC-xml#charencoding" target="_top">standard</a>.</p></td></tr></table></div><p>
</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. &quot;story&quot; is the root type of the documents used in this
tutorial.</p></td></tr></table></div><p>
</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>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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.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
<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.57.0"><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" lang="en"><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 &quot;keyword&quot; contained within element called &quot;story&quot;. 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">
</p><pre class="programlisting">
<a name="getchildnode"></a><img src="images/callouts/1.png" alt="1" border="0"> cur = cur-&gt;xmlChildrenNode;
<a name="huntstoryinfo"></a><img src="images/callouts/2.png" alt="2" border="0"> while (cur != NULL) {
if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)&quot;storyinfo&quot;))){
......@@ -15,18 +15,18 @@
cur = cur-&gt;next;
}
</pre>
</pre><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>cur</tt>. At this
</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>cur</tt>. At this
point, <tt>cur</tt> points at the document root, which is
the element &quot;story&quot;.</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
&quot;story&quot;, looking for one called &quot;storyinfo&quot;. That
is the element that will contain the &quot;keywords&quot; 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>
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><p>
<pre class="programlisting">
</p><pre class="programlisting">
void
parseStory (xmlDocPtr doc, xmlNodePtr cur) {
......@@ -39,12 +39,12 @@ parseStory (xmlDocPtr doc, xmlNodePtr cur) {
}
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
</pre><p>
</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
for one that matches the element we're interested in, in this case
&quot;keyword&quot;.</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 &quot;keyword&quot; 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-&gt;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>
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>
</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>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><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
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Writing element content</title><meta name="generator" content="DocBook XSL Stylesheets V1.57.0"><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" lang="en"><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
&#8212; 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 &quot;storyinfo&quot; 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>
<a href="apc.html" title="C. Code for Add Keyword Example">AppendixC</a></p><p>
The main difference in this example is in
<tt>parseStory</tt>:
<pre class="programlisting">
</p><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, &quot;keyword&quot;, 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>
</pre><p>
</p><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>
tree, specified by <tt>cur</tt>.</p></td></tr></table></div><p>
</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">
</p><pre class="programlisting">
xmlSaveFormatFile (docname, doc, 1);
</pre>
</pre><p>
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
......
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><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="ar01s07.html" title="Retrieving Attributes"></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="ar01s07.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
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Writing Attribute</title><meta name="generator" content="DocBook XSL Stylesheets V1.57.0"><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="ar01s07.html" title="Retrieving Attributes"></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="ar01s07.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><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>
document. Full code:<a href="apd.html" title="D. Code for Add Attribute Example">AppendixD</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">
data type we have not seen yet:
</p><pre class="programlisting">
xmlAttrPtr newattr;
</pre>
</pre><p>
We also need an extra xmlNodePtr:
<pre class="programlisting">
</p><pre class="programlisting">
xmlNodePtr newnode;
</pre>
</pre><p>
</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">
</p><pre class="programlisting">
<a name="addreferencenode"></a><img src="images/callouts/1.png" alt="1" border="0"> newnode = xmlNewTextChild (cur, NULL, &quot;reference&quot;, NULL);
<a name="addattributenode"></a><img src="images/callouts/2.png" alt="2" border="0"> newattr = xmlNewProp (newnode, &quot;uri&quot;, 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>
</pre><p>
</p><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><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="ar01s07.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">Retrieving Attributes</td></tr></table></div></body></html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Retrieving Attributes</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="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">Retrieving Attributes</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="apa.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="xmltutorialattribute"></a>Retrieving Attributes</h2></div></div><p>Retrieving the value of an attribute is similar to the previous
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Retrieving Attributes</title><meta name="generator" content="DocBook XSL Stylesheets V1.57.0"><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="ar01s08.html" title="Encoding Conversion"></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 Attributes</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="ar01s08.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="xmltutorialattribute"></a>Retrieving Attributes</h2></div></div><p>Retrieving the value of an attribute is similar to the previous
example in which we retrieved a node's text contents. In this case we'll
extract the value of the URI we added in the previous
section. Full code: <a href="ape.html" title="E. Code for Retrieving Attribute Value Example">Appendix E</a>.</p><p>
section. Full code: <a href="ape.html" title="E. Code for Retrieving Attribute Value Example">AppendixE</a>.</p><p>
The initial steps for this example are similar to the previous ones: parse
the doc, find the element you are interested in, then enter a function to
carry out the specific task required. In this case, we call
<tt>getReference</tt>:
<pre class="programlisting">
</p><pre class="programlisting">
void
getReference (xmlDocPtr doc, xmlNodePtr cur) {
......@@ -20,16 +20,16 @@ getReference (xmlDocPtr doc, xmlNodePtr cur) {
}
return;
}
</pre>
</pre><p>
<div class="calloutlist"><table border="0" summary="Callout list"><tr><td width="5%" valign="top" align="left"><a href="#getattributevalue"><img src="images/callouts/1.png" alt="1" border="0"></a> </td><td valign="top" align="left"><p>
</p><div class="calloutlist"><table border="0" summary="Callout list"><tr><td width="5%" valign="top" align="left"><a href="#getattributevalue"><img src="images/callouts/1.png" alt="1" border="0"></a> </td><td valign="top" align="left"><p>
The key function is <tt><a href="http://xmlsoft.org/html/libxml-tree.html#XMLGETPROP" target="_top">xmlGetProp</a></tt>, which returns an
<tt>xmlChar</tt> containing the attribute's value. In this case,
we just print it out.
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
</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>Note</th></tr><tr><td colspan="2" align="left" valign="top"><p>
If you are using a DTD that declares a fixed or
default value for the attribute, this function will retrieve it.
</p></div>
</p></td></tr></table></div>
</p></td></tr></table></div><p>
</p></td></tr></table></div><p>
</p></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="apa.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">A. Sample Document</td></tr></table></div></body></html>
</p></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="ar01s08.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">Encoding Conversion</td></tr></table></div></body></html>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment