1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-03-27 18:50:07 +03:00

doc/tutorial/apa.html doc/tutorial/apb.html doc/tutorial/apc.html

Thu Jul 24 15:46:02 MDT 2003 John Fleck <jfleck@inkstain.net>

	* doc/tutorial/apa.html
	* doc/tutorial/apb.html
	* doc/tutorial/apc.html
	* doc/tutorial/apd.html
	* doc/tutorial/ape.html
	* doc/tutorial/apf.html
	* doc/tutorial/apg.html
	* doc/tutorial/aph.html
	* doc/tutorial/ar01s02.html
	* doc/tutorial/ar01s03.html
	* doc/tutorial/ar01s04.html
	* doc/tutorial/ar01s05.html
	* doc/tutorial/ar01s06.html
	* doc/tutorial/ar01s07.html
	* doc/tutorial/ar01s08.html
	* doc/tutorial/index.html
	* doc/tutorial/ix01.html
	* doc/tutorial/xmltutorial.pdf
	* doc/tutorial/xmltutorial.xml
	update tutorial with XPath example
This commit is contained in:
MDT 2003 John Fleck 2003-07-24 21:48:30 +00:00 committed by John Fleck
parent 597bc48bab
commit 63f3a47dbf
20 changed files with 1905 additions and 1416 deletions

View File

@ -1,3 +1,27 @@
Thu Jul 24 15:46:02 MDT 2003 John Fleck <jfleck@inkstain.net>
* doc/tutorial/apa.html
* doc/tutorial/apb.html
* doc/tutorial/apc.html
* doc/tutorial/apd.html
* doc/tutorial/ape.html
* doc/tutorial/apf.html
* doc/tutorial/apg.html
* doc/tutorial/aph.html
* doc/tutorial/ar01s02.html
* doc/tutorial/ar01s03.html
* doc/tutorial/ar01s04.html
* doc/tutorial/ar01s05.html
* doc/tutorial/ar01s06.html
* doc/tutorial/ar01s07.html
* doc/tutorial/ar01s08.html
* doc/tutorial/index.html
* doc/tutorial/ix01.html
* doc/tutorial/xmltutorial.pdf
* doc/tutorial/xmltutorial.xml
update tutorial with XPath example
Thu Jul 24 17:07:06 IST 2003 Daniel Veillard <daniel@veillard.com>
* SAX.c parser.c: fixing a bug about a special case of namespace

View File

@ -1,9 +1,8 @@
<!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. Compilation</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><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. 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">A. Compilation</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><div><h2 class="title"><a name="compilation"></a>A. Compilation</h2></div></div><div></div></div><p><a class="indexterm" name="id2814889"></a>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>A. Compilation</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="ar01s09.html" title="Encoding Conversion"><link rel="next" href="apb.html" title="B. 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">A. Compilation</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s09.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><div><h2 class="title"><a name="compilation"></a>A. Compilation</h2></div></div><div></div></div><p><a class="indexterm" name="id2815236"></a>
<span class="application">Libxml</span> includes a script,
<span class="application">xml2-config</span>, that can be used to generate
flags for compilation and linking of programs written with the
library. For pre-processor and compiler flags, use <b class="command">xml2-config
--cflags</b>. For library linking flags, use <b class="command">xml2-config
--libs</b>. Other options are available using <b class="command">xml2-config
--help</b>.</p></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. Sample Document</td></tr></table></div></body></html>
--help</b>.</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s09.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. Sample Document</td></tr></table></div></body></html>

View File

@ -1,6 +1,5 @@
<!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. Sample Document</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><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. Compilation"><link rel="next" href="apc.html" title="C. 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">B. Sample Document</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><div><h2 class="title"><a name="sampledoc"></a>B. Sample Document</h2></div></div><div></div></div><pre class="programlisting">
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;?xml version="1.0"?&gt;
&lt;story&gt;
&lt;storyinfo&gt;
&lt;author&gt;John Fleck&lt;/author&gt;

View File

@ -1,5 +1,4 @@
<!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 Keyword Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><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. Sample Document"><link rel="next" href="apd.html" title="D. 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">C. Code for 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><div><h2 class="title"><a name="keywordappendix"></a>C. Code for Keyword Example</h2></div></div><div></div></div><p>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>C. Code for Keyword Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><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. Sample Document"><link rel="next" href="apd.html" title="D. Code for XPath 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 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><div><h2 class="title"><a name="keywordappendix"></a>C. Code for Keyword Example</h2></div></div><div></div></div><p>
</p><pre class="programlisting">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
@ -13,9 +12,9 @@ parseStory (xmlDocPtr doc, xmlNodePtr cur) {
xmlChar *key;
cur = cur-&gt;xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)&quot;keyword&quot;))) {
if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)"keyword"))) {
key = xmlNodeListGetString(doc, cur-&gt;xmlChildrenNode, 1);
printf(&quot;keyword: %s\n&quot;, key);
printf("keyword: %s\n", key);
xmlFree(key);
}
cur = cur-&gt;next;
@ -32,27 +31,27 @@ parseDoc(char *docname) {
doc = xmlParseFile(docname);
if (doc == NULL ) {
fprintf(stderr,&quot;Document not parsed successfully. \n&quot;);
fprintf(stderr,"Document not parsed successfully. \n");
return;
}
cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
fprintf(stderr,&quot;empty document\n&quot;);
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return;
}
if (xmlStrcmp(cur-&gt;name, (const xmlChar *) &quot;story&quot;)) {
fprintf(stderr,&quot;document of the wrong type, root node != story&quot;);
if (xmlStrcmp(cur-&gt;name, (const xmlChar *) "story")) {
fprintf(stderr,"document of the wrong type, root node != story");
xmlFreeDoc(doc);
return;
}
cur = cur-&gt;xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)&quot;storyinfo&quot;))){
if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)"storyinfo"))){
parseStory (doc, cur);
}
@ -69,7 +68,7 @@ main(int argc, char **argv) {
char *docname;
if (argc &lt;= 1) {
printf(&quot;Usage: %s docname\n&quot;, argv[0]);
printf("Usage: %s docname\n", argv[0]);
return(0);
}
@ -80,4 +79,4 @@ main(int argc, char **argv) {
}
</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. Sample Document </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 Keyword Example</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="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. Sample Document </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 XPath Example</td></tr></table></div></body></html>

View File

@ -1,79 +1,68 @@
<!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 Keyword Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><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 Keyword Example"><link rel="next" href="ape.html" title="E. 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">D. Code for Add Keyword 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><div><h2 class="title"><a name="addkeywordappendix"></a>D. Code for Add Keyword Example</h2></div></div><div></div></div><p>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>D. Code for XPath Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><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 Keyword Example"><link rel="next" href="ape.html" title="E. 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 XPath 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><div><h2 class="title"><a name="xpathappendix"></a>D. Code for XPath Example</h2></div></div><div></div></div><p>
</p><pre class="programlisting">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;libxml/xmlmemory.h&gt;
#include &lt;libxml/parser.h&gt;
void
parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword) {
xmlNewTextChild (cur, NULL, &quot;keyword&quot;, keyword);
return;
}
#include &lt;libxml/xpath.h&gt;
xmlDocPtr
parseDoc(char *docname, char *keyword) {
getdoc (char *docname) {
xmlDocPtr doc;
xmlNodePtr cur;
doc = xmlParseFile(docname);
if (doc == NULL ) {
fprintf(stderr,&quot;Document not parsed successfully. \n&quot;);
return (NULL);
fprintf(stderr,"Document not parsed successfully. \n");
return NULL;
}
cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
fprintf(stderr,&quot;empty document\n&quot;);
xmlFreeDoc(doc);
return (NULL);
}
if (xmlStrcmp(cur-&gt;name, (const xmlChar *) &quot;story&quot;)) {
fprintf(stderr,&quot;document of the wrong type, root node != story&quot;);
xmlFreeDoc(doc);
return (NULL);
}
cur = cur-&gt;xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)&quot;storyinfo&quot;))){
parseStory (doc, cur, keyword);
}
cur = cur-&gt;next;
}
return(doc);
return doc;
}
xmlXPathObjectPtr
getnodeset (xmlDocPtr doc, xmlChar *xpath){
xmlXPathContextPtr context;
xmlXPathObjectPtr result;
context = xmlXPathNewContext(doc);
result = xmlXPathEvalExpression(xpath, context);
if(xmlXPathNodeSetIsEmpty(result-&gt;nodesetval)){
printf("No result\n");
return NULL;
}
xmlXPathFreeContext(context);
return result;
}
int
main(int argc, char **argv) {
char *docname;
char *keyword;
xmlDocPtr doc;
if (argc &lt;= 2) {
printf(&quot;Usage: %s docname, keyword\n&quot;, argv[0]);
xmlChar *xpath = ("//keyword");
xmlNodeSetPtr nodeset;
xmlXPathObjectPtr result;
int i;
xmlChar *keyword;
if (argc &lt;= 1) {
printf("Usage: %s docname\n", argv[0]);
return(0);
}
docname = argv[1];
keyword = argv[2];
doc = parseDoc (docname, keyword);
if (doc != NULL) {
xmlSaveFormatFile (docname, doc, 0);
xmlFreeDoc(doc);
doc = getdoc(docname);
result = getnodeset (doc, xpath);
if (result) {
nodeset = result-&gt;nodesetval;
for (i=0; i &lt; nodeset-&gt;nodeNr; i++) {
keyword = xmlNodeListGetString(doc, nodeset-&gt;nodeTab[i]-&gt;xmlChildrenNode, 1);
printf("keyword: %s\n", keyword);
}
}
xmlXPathFreeObject (result);
xmlFree(keyword);
xmlFreeDoc(doc);
xmlCleanupParser();
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="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 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 Add Attribute Example</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="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 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 Add Keyword Example</td></tr></table></div></body></html>

View File

@ -1,5 +1,4 @@
<!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 Add Attribute Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><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 Keyword Example"><link rel="next" href="apf.html" title="F. 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">E. Code for Add Attribute 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><div><h2 class="title"><a name="addattributeappendix"></a>E. Code for Add Attribute Example</h2></div></div><div></div></div><p>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>E. Code for Add Keyword Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><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 XPath Example"><link rel="next" href="apf.html" title="F. 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 Add Keyword 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><div><h2 class="title"><a name="addkeywordappendix"></a>E. Code for Add Keyword Example</h2></div></div><div></div></div><p>
</p><pre class="programlisting">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
@ -7,38 +6,48 @@
#include &lt;libxml/xmlmemory.h&gt;
#include &lt;libxml/parser.h&gt;
void
parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword) {
xmlNewTextChild (cur, NULL, "keyword", keyword);
return;
}
xmlDocPtr
parseDoc(char *docname, char *uri) {
parseDoc(char *docname, char *keyword) {
xmlDocPtr doc;
xmlNodePtr cur;
xmlNodePtr newnode;
xmlAttrPtr newattr;
doc = xmlParseFile(docname);
if (doc == NULL ) {
fprintf(stderr,&quot;Document not parsed successfully. \n&quot;);
fprintf(stderr,"Document not parsed successfully. \n");
return (NULL);
}
cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
fprintf(stderr,&quot;empty document\n&quot;);
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return (NULL);
}
if (xmlStrcmp(cur-&gt;name, (const xmlChar *) &quot;story&quot;)) {
fprintf(stderr,&quot;document of the wrong type, root node != story&quot;);
if (xmlStrcmp(cur-&gt;name, (const xmlChar *) "story")) {
fprintf(stderr,"document of the wrong type, root node != story");
xmlFreeDoc(doc);
return (NULL);
}
newnode = xmlNewTextChild (cur, NULL, &quot;reference&quot;, NULL);
newattr = xmlNewProp (newnode, &quot;uri&quot;, uri);
cur = cur-&gt;xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)"storyinfo"))){
parseStory (doc, cur, keyword);
}
cur = cur-&gt;next;
}
return(doc);
}
@ -46,23 +55,24 @@ int
main(int argc, char **argv) {
char *docname;
char *uri;
char *keyword;
xmlDocPtr doc;
if (argc &lt;= 2) {
printf(&quot;Usage: %s docname, uri\n&quot;, argv[0]);
printf("Usage: %s docname, keyword\n", argv[0]);
return(0);
}
docname = argv[1];
uri = argv[2];
doc = parseDoc (docname, uri);
keyword = argv[2];
doc = parseDoc (docname, keyword);
if (doc != NULL) {
xmlSaveFormatFile (docname, doc, 1);
xmlSaveFormatFile (docname, doc, 0);
xmlFreeDoc(doc);
}
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="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 Keyword 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 Retrieving Attribute Value Example</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="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 XPath 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 Add Attribute Example</td></tr></table></div></body></html>

View File

@ -1,5 +1,4 @@
<!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 Retrieving Attribute Value Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><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 Add Attribute Example"><link rel="next" href="apg.html" title="G. 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">F. Code for Retrieving Attribute Value 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><div><h2 class="title"><a name="getattributeappendix"></a>F. Code for Retrieving Attribute Value Example</h2></div></div><div></div></div><p>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>F. Code for Add Attribute Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><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 Add Keyword Example"><link rel="next" href="apg.html" title="G. 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">F. Code for Add Attribute 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><div><h2 class="title"><a name="addattributeappendix"></a>F. Code for Add Attribute Example</h2></div></div><div></div></div><p>
</p><pre class="programlisting">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
@ -7,70 +6,62 @@
#include &lt;libxml/xmlmemory.h&gt;
#include &lt;libxml/parser.h&gt;
void
getReference (xmlDocPtr doc, xmlNodePtr cur) {
xmlChar *uri;
cur = cur-&gt;xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)&quot;reference&quot;))) {
uri = xmlGetProp(cur, &quot;uri&quot;);
printf(&quot;uri: %s\n&quot;, uri);
xmlFree(uri);
}
cur = cur-&gt;next;
}
return;
}
void
parseDoc(char *docname) {
xmlDocPtr
parseDoc(char *docname, char *uri) {
xmlDocPtr doc;
xmlNodePtr cur;
xmlNodePtr newnode;
xmlAttrPtr newattr;
doc = xmlParseFile(docname);
if (doc == NULL ) {
fprintf(stderr,&quot;Document not parsed successfully. \n&quot;);
return;
fprintf(stderr,"Document not parsed successfully. \n");
return (NULL);
}
cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
fprintf(stderr,&quot;empty document\n&quot;);
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return;
return (NULL);
}
if (xmlStrcmp(cur-&gt;name, (const xmlChar *) &quot;story&quot;)) {
fprintf(stderr,&quot;document of the wrong type, root node != story&quot;);
if (xmlStrcmp(cur-&gt;name, (const xmlChar *) "story")) {
fprintf(stderr,"document of the wrong type, root node != story");
xmlFreeDoc(doc);
return;
return (NULL);
}
getReference (doc, cur);
xmlFreeDoc(doc);
return;
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 &lt;= 1) {
printf(&quot;Usage: %s docname\n&quot;, argv[0]);
if (argc &lt;= 2) {
printf("Usage: %s docname, uri\n", argv[0]);
return(0);
}
docname = argv[1];
parseDoc (docname);
uri = argv[2];
doc = parseDoc (docname, uri);
if (doc != NULL) {
xmlSaveFormatFile (docname, doc, 1);
xmlFreeDoc(doc);
}
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 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"> G. Code for Encoding Conversion Example</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="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 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"> G. Code for Retrieving Attribute Value Example</td></tr></table></div></body></html>

View File

@ -1,77 +1,75 @@
<!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. Code for Encoding Conversion Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><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 Retrieving Attribute Value Example"><link rel="next" href="aph.html" title="H. 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">G. Code for Encoding Conversion Example</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"> <a accesskey="n" href="aph.html">Next</a></td></tr></table><hr></div><div class="appendix" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="convertappendix"></a>G. Code for Encoding Conversion Example</h2></div></div><div></div></div><p>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>G. Code for Retrieving Attribute Value Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><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 Add Attribute Example"><link rel="next" href="aph.html" title="H. 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. Code for Retrieving Attribute Value Example</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"> <a accesskey="n" href="aph.html">Next</a></td></tr></table><hr></div><div class="appendix" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="getattributeappendix"></a>G. Code for Retrieving Attribute Value Example</h2></div></div><div></div></div><p>
</p><pre class="programlisting">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;libxml/xmlmemory.h&gt;
#include &lt;libxml/parser.h&gt;
void
getReference (xmlDocPtr doc, xmlNodePtr cur) {
unsigned char*
convert (unsigned char *in, char *encoding)
{
unsigned char *out;
int ret,size,out_size,temp;
xmlCharEncodingHandlerPtr handler;
xmlChar *uri;
cur = cur-&gt;xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)"reference"))) {
uri = xmlGetProp(cur, "uri");
printf("uri: %s\n", uri);
xmlFree(uri);
}
cur = cur-&gt;next;
}
return;
}
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);
}
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-&gt;name, (const xmlChar *) "story")) {
fprintf(stderr,"document of the wrong type, root node != story");
xmlFreeDoc(doc);
return;
}
getReference (doc, cur);
xmlFreeDoc(doc);
return;
}
int
main(int argc, char **argv) {
unsigned char *content, *out;
xmlDocPtr doc;
xmlNodePtr rootnode;
char *encoding = &quot;ISO-8859-1&quot;;
char *docname;
if (argc &lt;= 1) {
printf(&quot;Usage: %s content\n&quot;, argv[0]);
printf("Usage: %s docname\n", 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);
docname = argv[1];
parseDoc (docname);
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="apf.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="aph.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">F. 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"> H. Acknowledgements</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="apf.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="aph.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">F. 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"> H. Code for Encoding Conversion Example</td></tr></table></div></body></html>

View File

@ -1,5 +1,76 @@
<!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>H. Acknowledgements</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="apg.html" title="G. Code for Encoding Conversion Example"><link rel="next" href="ix01.html" title="Index"></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">H. Acknowledgements</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apg.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ix01.html">Next</a></td></tr></table><hr></div><div class="appendix" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="id2815353"></a>H. Acknowledgements</h2></div></div><div></div></div><p>A number of people have generously offered feedback, code and
suggested improvements to this tutorial. In no particular order:
<span class="simplelist">Daniel Veillard, Marcus Labib Iskander, Christopher R. Harris, Igor Zlatkovic, Niraj Tolia</span>
</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="apg.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="ix01.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">G. 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"> Index</td></tr></table></div></body></html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>H. Code for Encoding Conversion Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="apg.html" title="G. Code for Retrieving Attribute Value Example"><link rel="next" href="api.html" title="I. 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">H. Code for Encoding Conversion Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apg.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="api.html">Next</a></td></tr></table><hr></div><div class="appendix" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="convertappendix"></a>H. Code for Encoding Conversion Example</h2></div></div><div></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("conversion wasn't successful.\n");
} else {
printf("conversion wasn't successful. converted: %i octets.\n",temp);
}
free(out);
out = NULL;
} else {
out = realloc(out,out_size+1);
out[out_size]=0; /*null terminating out*/
}
} else {
printf("no mem\n");
}
return (out);
}
int
main(int argc, char **argv) {
unsigned char *content, *out;
xmlDocPtr doc;
xmlNodePtr rootnode;
char *encoding = "ISO-8859-1";
if (argc &lt;= 1) {
printf("Usage: %s content\n", argv[0]);
return(0);
}
content = argv[1];
out = convert(content, encoding);
doc = xmlNewDoc ("1.0");
rootnode = xmlNewDocNode(doc, NULL, (const xmlChar*)"root", out);
xmlDocSetRootElement(doc, rootnode);
xmlSaveFormatFileEnc("-", 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="apg.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="api.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">G. 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"> I. Acknowledgements</td></tr></table></div></body></html>

View File

@ -1,14 +1,13 @@
<!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.60.1"><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><div><h2 class="title" style="clear: both"><a name="xmltutorialdatatypes"></a>Data Types</h2></div></div><div></div></div><p><span class="application">Libxml</span> 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>
</p><div class="variablelist"><dl><dt><span class="term"><a class="indexterm" name="id2816027"></a>
</p><div class="variablelist"><dl><dt><span class="term"><a class="indexterm" name="id2815889"></a>
<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 <span class="application">libxml's</span>
functions. More information on encoding is available on the <a href="http://www.xmlsoft.org/encoding.html" target="_top"><span class="application">libxml</span> encoding support web page</a>.</p></dd><dt><span class="term"><a class="indexterm" name="id2816083"></a>
functions. More information on encoding is available on the <a href="http://www.xmlsoft.org/encoding.html" target="_top"><span class="application">libxml</span> encoding support web page</a>.</p></dd><dt><span class="term"><a class="indexterm" name="id2815945"></a>
<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 class="indexterm" name="id2816125"></a>
is a pointer to the structure.</p></dd><dt><span class="term"><a class="indexterm" name="id2815986"></a>
<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>

View File

@ -1,4 +1,3 @@
<!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.60.1"><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><div><h2 class="title" style="clear: both"><a name="xmltutorialparsing"></a>Parsing the file</h2></div></div><div></div></div><p><a class="indexterm" name="fileparsing"></a>
Parsing the file requires only the name of the file and a single
function call, plus error checking. Full code: <a href="apc.html" title="C. Code for Keyword Example">Appendix C, <i>Code for Keyword Example</i></a></p><p>
@ -9,20 +8,20 @@ Parsing the file requires only the name of the file and a single
<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,&quot;Document not parsed successfully. \n&quot;);
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,&quot;empty document\n&quot;);
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-&gt;name, (const xmlChar *) &quot;story&quot;)) {
fprintf(stderr,&quot;document of the wrong type, root node != story&quot;);
<a name="checkroottype"></a><img src="images/callouts/7.png" alt="7" border="0"> if (xmlStrcmp(cur-&gt;name, (const xmlChar *) "story")) {
fprintf(stderr,"document of the wrong type, root node != story");
xmlFreeDoc(doc);
return;
}
@ -32,7 +31,7 @@ Parsing the file requires only the name of the file and a single
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, <span class="application">libxml</span> 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 align="left">Note</th></tr><tr><td colspan="2" align="left" valign="top"><p><a class="indexterm" name="id2867695"></a>
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td colspan="2" align="left" valign="top"><p><a class="indexterm" name="id2816318"></a>
One common example of an error at this point is improper
handling of encoding. The <span class="acronym">XML</span> standard requires
documents stored with an encoding other than UTF-8 or UTF-16 to
@ -42,7 +41,7 @@ One common example of an error at this point is improper
you. More information on <span class="acronym">XML's</span> 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
type. "story" is the root type of the documents used in this
tutorial.</p></td></tr></table></div><p>
<a class="indexterm" name="id2867789"></a>
<a class="indexterm" name="id2816412"></a>
</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>

View File

@ -1,15 +1,14 @@
<!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.60.1"><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><div><h2 class="title" style="clear: both"><a name="xmltutorialgettext"></a>Retrieving Element Content</h2></div></div><div></div></div><p><a class="indexterm" name="id2867816"></a>
<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.60.1"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="ar01s03.html" title="Parsing the file"><link rel="next" href="ar01s05.html" title="Using XPath to Retrieve Element Content"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Retrieving Element Content</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s03.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ar01s05.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="xmltutorialgettext"></a>Retrieving Element Content</h2></div></div><div></div></div><p><a class="indexterm" name="id2816439"></a>
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
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 class="varname">doc</tt>
and an xmlNodPtr called <tt class="varname">cur</tt>.</p><p>
</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;))){
if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)"storyinfo"))){
parseStory (doc, cur);
}
@ -19,9 +18,9 @@ Retrieving the content of an element involves traversing the document
</p><div class="calloutlist"><table border="0" summary="Callout list"><tr><td width="5%" valign="top" align="left"><a href="#getchildnode"><img src="images/callouts/1.png" alt="1" border="0"></a> </td><td valign="top" align="left"><p>Get the first child node of <tt class="varname">cur</tt>. At this
point, <tt class="varname">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
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 <span class="application">libxml</span> string
comparison
function, <tt class="function"><a href="http://xmlsoft.org/html/libxml-parser.html#XMLSTRCMP" target="_top">xmlStrcmp</a></tt>. If there is a match, it calls the function <tt class="function">parseStory</tt>.</p></td></tr></table></div><p>
@ -33,9 +32,9 @@ parseStory (xmlDocPtr doc, xmlNodePtr cur) {
xmlChar *key;
<a name="anothergetchild"></a><img src="images/callouts/1.png" alt="1" border="0"> cur = cur-&gt;xmlChildrenNode;
<a name="findkeyword"></a><img src="images/callouts/2.png" alt="2" border="0"> while (cur != NULL) {
if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)&quot;keyword&quot;))) {
if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)"keyword"))) {
<a name="foundkeyword"></a><img src="images/callouts/3.png" alt="3" border="0"> key = xmlNodeListGetString(doc, cur-&gt;xmlChildrenNode, 1);
printf(&quot;keyword: %s\n&quot;, key);
printf("keyword: %s\n", key);
xmlFree(key);
}
cur = cur-&gt;next;
@ -45,11 +44,11 @@ parseStory (xmlDocPtr doc, xmlNodePtr cur) {
</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
"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 <span class="acronym">XML</span>, the text
contained within an element is a child node of that element, so we
turn to <tt class="varname">cur-&gt;xmlChildrenNode</tt>. To retrieve it, we
use the function <tt class="function"><a href="http://xmlsoft.org/html/libxml-tree.html#XMLNODELISTGETSTRING" target="_top">xmlNodeListGetString</a></tt>, which also takes the <tt class="varname">doc</tt> pointer as an argument. In this case, we just print it out.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td colspan="2" align="left" valign="top"><p>Because <tt class="function">xmlNodeListGetString</tt> allocates
memory for the string it returns, you must use
<tt class="function">xmlFree</tt> to free it.</p></td></tr></table></div></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>
</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s03.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ar01s05.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Parsing the file </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Using XPath to Retrieve Element Content</td></tr></table></div></body></html>

View File

@ -1,36 +1,55 @@
<!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.60.1"><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><div><h2 class="title" style="clear: both"><a name="xmltutorialwritingcontent"></a>Writing element content</h2></div></div><div></div></div><p><a class="indexterm" name="id2868124"></a>
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="apd.html" title="D. Code for Add Keyword Example">Appendix D, <i>Code for Add Keyword Example</i></a></p><p>
The main difference in this example is in
<tt class="function">parseStory</tt>:
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Using XPath to Retrieve Element Content</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><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 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">Using XPath to Retrieve 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><div><h2 class="title" style="clear: both"><a name="xmltutorialxpath"></a>Using XPath to Retrieve Element Content</h2></div></div><div></div></div><p>In addition to walking the document tree to find an element,
<span class="application">Libxml2</span> includes support for
use of <span class="application">XPath</span> expressions to retrieve sets of
nodes that match a specified criteria. Full documentation of the
<span class="application">XPath</span> <span class="acronym">API</span> is <a href="http://xmlsoft.org/html/libxml-xpath.html" target="_top">here</a>.
</p><p><span class="application">XPath</span> allows searching through a document
for nodes that match specified criteria. In the example below we search
through a document for the contents of all <tt class="varname">keyword</tt>
elements.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td colspan="2" align="left" valign="top"><p>A full discussion of <span class="application">XPath</span> is beyond
the scope of this document. For details on its use, see the <a href="http://www.w3.org/TR/xpath" target="_top">XPath specification</a>.</p></td></tr></table></div><p>
Full code for this example is at <a href="apd.html" title="D. Code for XPath Example">Appendix D, <i>Code for XPath Example</i></a>.
</p><p>Using <span class="application">XPath</span> requires setting up an
xmlXPathContext and then supplying the <span class="application">XPath</span>
expression and the context to the
<tt class="function">xmlXPathEvalExpression</tt> function. The function returns
an xmlXPathObjectPtr, which includes the set of nodes satisfying the
<span class="application">XPath</span> expression.</p><p>
</p><pre class="programlisting">
void
parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword) {
xmlXPathObjectPtr
getnodeset (xmlDocPtr doc, xmlChar *xpath){
<a name="cocontext"></a><img src="images/callouts/1.png" alt="1" border="0">xmlXPathContextPtr context;
xmlXPathObjectPtr result;
<a name="addkeyword"></a><img src="images/callouts/1.png" alt="1" border="0"> xmlNewTextChild (cur, NULL, &quot;keyword&quot;, keyword);
return;
}
<a name="cocreatecontext"></a><img src="images/callouts/2.png" alt="2" border="0">context = xmlXPathNewContext(doc);
<a name="corunxpath"></a><img src="images/callouts/3.png" alt="3" border="0">result = xmlXPathEvalExpression(xpath, context);
<a name="cocheckxpathresult"></a><img src="images/callouts/4.png" alt="4" border="0">if(xmlXPathNodeSetIsEmpty(result-&gt;nodesetval)){
printf("No result\n");
return NULL;
}
xmlXPathFreeContext(context);
return result;
</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 class="function"><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, specified by <tt class="varname">cur</tt>.</p></td></tr></table></div><p>
</p><p>
<a class="indexterm" name="id2814166"></a>
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.
</p><div class="calloutlist"><table border="0" summary="Callout list"><tr><td width="5%" valign="top" align="left"><a href="#cocontext"><img src="images/callouts/1.png" alt="1" border="0"></a> </td><td valign="top" align="left"><p>First we declare our variables.</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#cocreatecontext"><img src="images/callouts/2.png" alt="2" border="0"></a> </td><td valign="top" align="left"><p>Initialize the <tt class="varname">context</tt> variable.</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#corunxpath"><img src="images/callouts/3.png" alt="3" border="0"></a> </td><td valign="top" align="left"><p>Apply the <span class="application">XPath</span> expression.</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#cocheckxpathresult"><img src="images/callouts/4.png" alt="4" border="0"></a> </td><td valign="top" align="left"><p>Check the result.</p></td></tr></table></div><p>
</p><p>The xmlPathObjectPtr returned by the function contains a set of nodes
and other information needed to iterate through the set and act on the
results. For this example, our functions returns the
<tt class="varname">xmlXPathObjectPtr</tt>. We use it to print the contents of
<tt class="varname">keyword</tt> nodes in our document. The node set object
includes the number of elements in the set (<tt class="varname">nodeNr</tt>) and
an array of nodes (<tt class="varname">nodeTab</tt>):
</p><pre class="programlisting">
xmlSaveFormatFile (docname, doc, 1);
<a name="conodesetcounter"></a><img src="images/callouts/1.png" alt="1" border="0">for (i=0; i &lt; nodeset-&gt;nodeNr; i++) {
<a name="coprintkeywords"></a><img src="images/callouts/2.png" alt="2" border="0">keyword = xmlNodeListGetString(doc, nodeset-&gt;nodeTab[i]-&gt;xmlChildrenNode, 1);
printf("keyword: %s\n", keyword);
}
</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
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>
</p><div class="calloutlist"><table border="0" summary="Callout list"><tr><td width="5%" valign="top" align="left"><a href="#conodesetcounter"><img src="images/callouts/1.png" alt="1" border="0"></a> </td><td valign="top" align="left"><p>The value of <tt class="varname">nodeset-&gt;Nr</tt> holds the number of
elements in the node set. Here we use it to iterate through the array.</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#coprintkeywords"><img src="images/callouts/2.png" alt="2" border="0"></a> </td><td valign="top" align="left"><p>Here we print the contents of each of the nodes returned.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td colspan="2" align="left" valign="top"><p>Note that we are printing the child node of the node that is
returned, because the contents of the <tt class="varname">keyword</tt>
element are a child text node.</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="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 element content</td></tr></table></div></body></html>

View File

@ -1,31 +1,35 @@
<!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.60.1"><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><div><h2 class="title" style="clear: both"><a name="xmltutorialwritingattribute"></a>Writing Attribute</h2></div></div><div></div></div><p><a class="indexterm" name="id2814216"></a>
Writing an attribute is similar to writing text to a new element. In
this case, we'll add a reference <span class="acronym">URI</span> to our
document. Full code:<a href="ape.html" title="E. Code for Add Attribute Example">Appendix E, <i>Code for Add Attribute Example</i></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 class="function">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
data type we have not seen yet:
</p><pre class="programlisting">
xmlAttrPtr newattr;
</pre><p>
We also need an extra xmlNodePtr:
</p><pre class="programlisting">
xmlNodePtr newnode;
</pre><p>
</p><p>
The rest of <tt class="function">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:
<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.60.1"><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="Using XPath to Retrieve Element Content"><link rel="next" href="ar01s07.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="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><div><h2 class="title" style="clear: both"><a name="xmltutorialwritingcontent"></a>Writing element content</h2></div></div><div></div></div><p><a class="indexterm" name="id2814412"></a>
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 "storyinfo" element and
this time insert a keyword. Then we'll write the file to disk. Full code:
<a href="ape.html" title="E. Code for Add Keyword Example">Appendix E, <i>Code for Add Keyword Example</i></a></p><p>
The main difference in this example is in
<tt class="function">parseStory</tt>:
</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);
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><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 class="varname">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>
</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 class="function"><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, specified by <tt class="varname">cur</tt>.</p></td></tr></table></div><p>
</p><p>
<a class="indexterm" name="id2814512"></a>
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.
</p><pre class="programlisting">
xmlSaveFormatFile (docname, doc, 1);
</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
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="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">Using XPath to Retrieve 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>

View File

@ -1,39 +1,30 @@
<!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.60.1"><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><div><h2 class="title" style="clear: both"><a name="xmltutorialattribute"></a>Retrieving Attributes</h2></div></div><div></div></div><p><a class="indexterm" name="id2814383"></a>
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 <span class="acronym">URI</span> we added in the previous
section. Full code: <a href="apf.html" title="F. Code for Retrieving Attribute Value Example">Appendix F, <i>Code for Retrieving Attribute Value Example</i></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 class="function">getReference</tt>:
<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.60.1"><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 element content"><link rel="next" href="ar01s08.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="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><div><h2 class="title" style="clear: both"><a name="xmltutorialwritingattribute"></a>Writing Attribute</h2></div></div><div></div></div><p><a class="indexterm" name="id2814563"></a>
Writing an attribute is similar to writing text to a new element. In
this case, we'll add a reference <span class="acronym">URI</span> to our
document. Full code:<a href="apf.html" title="F. Code for Add Attribute Example">Appendix F, <i>Code for Add Attribute Example</i></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 class="function">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
data type we have not seen yet:
</p><pre class="programlisting">
void
getReference (xmlDocPtr doc, xmlNodePtr cur) {
xmlChar *uri;
cur = cur-&gt;xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)&quot;reference&quot;))) {
<a name="getattributevalue"></a><img src="images/callouts/1.png" alt="1" border="0"> uri = xmlGetProp(cur, &quot;uri&quot;);
printf(&quot;uri: %s\n&quot;, uri);
xmlFree(uri);
}
cur = cur-&gt;next;
}
return;
}
xmlAttrPtr newattr;
</pre><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 class="function"><a href="http://xmlsoft.org/html/libxml-tree.html#XMLGETPROP" target="_top">xmlGetProp</a></tt>, which returns an
<tt class="varname">xmlChar</tt> containing the attribute's value. In this case,
we just print it out.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td colspan="2" align="left" valign="top"><p>
If you are using a <span class="acronym">DTD</span> that declares a fixed or
default value for the attribute, this function will retrieve it.
</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="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>
We also need an extra xmlNodePtr:
</p><pre class="programlisting">
xmlNodePtr newnode;
</pre><p>
</p><p>
The rest of <tt class="function">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:
</p><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><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 class="varname">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="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 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>

View File

@ -1,64 +1,38 @@
<!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>Encoding Conversion</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><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="apa.html" title="A. Compilation"></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">Encoding Conversion</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="apa.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="xmltutorialconvert"></a>Encoding Conversion</h2></div></div><div></div></div><p><a class="indexterm" name="id2814514"></a>
Data encoding compatibility problems are one of the most common
difficulties encountered by programmers new to <span class="acronym">XML</span> in
general and <span class="application">libxml</span> in particular. Thinking
through the design of your application in light of this issue will help
avoid difficulties later. Internally, <span class="application">libxml</span>
stores and manipulates data in the UTF-8 format. Data used by your program
in other formats, such as the commonly used ISO-8859-1 encoding, must be
converted to UTF-8 before passing it to <span class="application">libxml</span>
functions. If you want your program's output in an encoding other than
UTF-8, you also must convert it.</p><p><span class="application">Libxml</span> uses
<span class="application">iconv</span> if it is available to convert
data. Without <span class="application">iconv</span>, only UTF-8, UTF-16 and
ISO-8859-1 can be used as external formats. With
<span class="application">iconv</span>, any format can be used provided
<span class="application">iconv</span> is able to convert it to and from
UTF-8. Currently <span class="application">iconv</span> supports about 150
different character formats with ability to convert from any to any. While
the actual number of supported formats varies between implementations, every
<span class="application">iconv</span> implementation is almost guaranteed to
support every format anyone has ever heard of.</p><div class="warning" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Warning"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="images/warning.png"></td><th align="left">Warning</th></tr><tr><td colspan="2" align="left" valign="top"><p>A common mistake is to use different formats for the internal data
in different parts of one's code. The most common case is an application
that assumes ISO-8859-1 to be the internal data format, combined with
<span class="application">libxml</span>, which assumes UTF-8 to be the
internal data format. The result is an application that treats internal
data differently, depending on which code section is executing. The one or
the other part of code will then, naturally, misinterpret the data.
</p></td></tr></table></div><p>This example constructs a simple document, then adds content provided
at the command line to the document's root element and outputs the results
to <tt class="filename">stdout</tt> in the proper encoding. For this example, we
use ISO-8859-1 encoding. The encoding of the string input at the command
line is converted from ISO-8859-1 to UTF-8. Full code: <a href="apg.html" title="G. Code for Encoding Conversion Example">Appendix G, <i>Code for Encoding Conversion Example</i></a></p><p>The conversion, encapsulated in the example code in the
<tt class="function">convert</tt> function, uses
<span class="application">libxml's</span>
<tt class="function">xmlFindCharEncodingHandler</tt> function:
<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.60.1"><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="Writing Attribute"><link rel="next" href="ar01s09.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="ar01s07.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ar01s09.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="xmltutorialattribute"></a>Retrieving Attributes</h2></div></div><div></div></div><p><a class="indexterm" name="id2814729"></a>
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 <span class="acronym">URI</span> we added in the previous
section. Full code: <a href="apg.html" title="G. Code for Retrieving Attribute Value Example">Appendix G, <i>Code for Retrieving Attribute Value Example</i></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 class="function">getReference</tt>:
</p><pre class="programlisting">
<a name="handlerdatatype"></a><img src="images/callouts/1.png" alt="1" border="0">xmlCharEncodingHandlerPtr handler;
<a name="calcsize"></a><img src="images/callouts/2.png" alt="2" border="0">size = (int)strlen(in)+1;
out_size = size*2-1;
out = malloc((size_t)out_size);
void
getReference (xmlDocPtr doc, xmlNodePtr cur) {
&#8230;
<a name="findhandlerfunction"></a><img src="images/callouts/3.png" alt="3" border="0">handler = xmlFindCharEncodingHandler(encoding);
&#8230;
<a name="callconversionfunction"></a><img src="images/callouts/4.png" alt="4" border="0">handler-&gt;input(out, &amp;out_size, in, &amp;temp);
&#8230;
<a name="outputencoding"></a><img src="images/callouts/5.png" alt="5" border="0">xmlSaveFormatFileEnc(&quot;-&quot;, doc, encoding, 1);
xmlChar *uri;
cur = cur-&gt;xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)"reference"))) {
<a name="getattributevalue"></a><img src="images/callouts/1.png" alt="1" border="0"> uri = xmlGetProp(cur, "uri");
printf("uri: %s\n", uri);
xmlFree(uri);
}
cur = cur-&gt;next;
}
return;
}
</pre><p>
</p><div class="calloutlist"><table border="0" summary="Callout list"><tr><td width="5%" valign="top" align="left"><a href="#handlerdatatype"><img src="images/callouts/1.png" alt="1" border="0"></a> </td><td valign="top" align="left"><p><tt class="varname">handler</tt> is declared as a pointer to an
<tt class="function">xmlCharEncodingHandler</tt> function.</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#calcsize"><img src="images/callouts/2.png" alt="2" border="0"></a> </td><td valign="top" align="left"><p>The <tt class="function">xmlCharEncodingHandler</tt> function needs
to be given the size of the input and output strings, which are
calculated here for strings <tt class="varname">in</tt> and
<tt class="varname">out</tt>.</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#findhandlerfunction"><img src="images/callouts/3.png" alt="3" border="0"></a> </td><td valign="top" align="left"><p><tt class="function">xmlFindCharEncodingHandler</tt> takes as its
argument the data's initial encoding and searches
<span class="application">libxml's</span> built-in set of conversion
handlers, returning a pointer to the function or NULL if none is
found.</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#callconversionfunction"><img src="images/callouts/4.png" alt="4" border="0"></a> </td><td valign="top" align="left"><p>The conversion function identified by <tt class="varname">handler</tt>
requires as its arguments pointers to the input and output strings,
along with the length of each. The lengths must be determined
separately by the application.</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#outputencoding"><img src="images/callouts/5.png" alt="5" border="0"></a> </td><td valign="top" align="left"><p>To output in a specified encoding rather than UTF-8, we use
<tt class="function">xmlSaveFormatFileEnc</tt>, specifying the
encoding.</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="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="apa.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"> A. Compilation</td></tr></table></div></body></html>
</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 class="function"><a href="http://xmlsoft.org/html/libxml-tree.html#XMLGETPROP" target="_top">xmlGetProp</a></tt>, which returns an
<tt class="varname">xmlChar</tt> containing the attribute's value. In this case,
we just print it out.
</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td colspan="2" align="left" valign="top"><p>
If you are using a <span class="acronym">DTD</span> that declares a fixed or
default value for the attribute, this function will retrieve it.
</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="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="ar01s09.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>

View File

@ -1,5 +1,4 @@
<!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>Libxml Tutorial</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><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" lang="en"><div class="titlepage"><div><div><h1 class="title"><a name="id2759548"></a>Libxml Tutorial</h1></div><div><div class="author"><h3 class="author"><span class="firstname">John</span> <span class="surname">Fleck</span></h3></div></div><div><p class="copyright">Copyright © 2002, 2003 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><tr><td align="left" colspan="2">Initial draft</td></tr><tr><td align="left">Revision 2</td><td align="left">June 12, 2002</td></tr><tr><td align="left" colspan="2">retrieving attribute value added</td></tr><tr><td align="left">Revision 3</td><td align="left">Aug. 31, 2002</td></tr><tr><td align="left" colspan="2">freeing memory fix</td></tr><tr><td align="left">Revision 4</td><td align="left">Nov. 10, 2002</td></tr><tr><td align="left" colspan="2">encoding discussion added</td></tr><tr><td align="left">Revision 5</td><td align="left">Dec. 15, 2002</td></tr><tr><td align="left" colspan="2">more memory freeing changes</td></tr><tr><td align="left">Revision 6</td><td align="left">Jan. 26. 2003</td></tr><tr><td align="left" colspan="2">add index</td></tr><tr><td align="left">Revision 7</td><td align="left">April 25, 2003</td></tr><tr><td align="left" colspan="2">add compilation appendix</td></tr></table></div></div></div><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 href="ar01s07.html">Retrieving Attributes</a></dt><dt><a href="ar01s08.html">Encoding Conversion</a></dt><dt>A. <a href="apa.html">Compilation</a></dt><dt>B. <a href="apb.html">Sample Document</a></dt><dt>C. <a href="apc.html">Code for Keyword Example</a></dt><dt>D. <a href="apd.html">Code for Add Keyword Example</a></dt><dt>E. <a href="ape.html">Code for Add Attribute Example</a></dt><dt>F. <a href="apf.html">Code for Retrieving Attribute Value Example</a></dt><dt>G. <a href="apg.html">Code for Encoding Conversion Example</a></dt><dt>H. <a href="aph.html">Acknowledgements</a></dt><dt>I. <a href="ix01.html">Index</a></dt</dl></div><div class="abstract"><p class="title"><b>Abstract</b></p><p>Libxml is a freely licensed C language library for handling
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Libxml Tutorial</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><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" lang="en"><div class="titlepage"><div><div><h1 class="title"><a name="id2759589"></a>Libxml Tutorial</h1></div><div><div class="author"><h3 class="author"><span class="firstname">John</span> <span class="surname">Fleck</span></h3></div></div><div><p class="copyright">Copyright © 2002, 2003 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><tr><td align="left" colspan="2">Initial draft</td></tr><tr><td align="left">Revision 2</td><td align="left">June 12, 2002</td></tr><tr><td align="left" colspan="2">retrieving attribute value added</td></tr><tr><td align="left">Revision 3</td><td align="left">Aug. 31, 2002</td></tr><tr><td align="left" colspan="2">freeing memory fix</td></tr><tr><td align="left">Revision 4</td><td align="left">Nov. 10, 2002</td></tr><tr><td align="left" colspan="2">encoding discussion added</td></tr><tr><td align="left">Revision 5</td><td align="left">Dec. 15, 2002</td></tr><tr><td align="left" colspan="2">more memory freeing changes</td></tr><tr><td align="left">Revision 6</td><td align="left">Jan. 26. 2003</td></tr><tr><td align="left" colspan="2">add index</td></tr><tr><td align="left">Revision 7</td><td align="left">April 25, 2003</td></tr><tr><td align="left" colspan="2">add compilation appendix</td></tr><tr><td align="left">Revision 8</td><td align="left">July 24, 2003</td></tr><tr><td align="left" colspan="2">add XPath example</td></tr></table></div></div></div><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">Using XPath to Retrieve Element Content</a></dt><dt><a href="ar01s06.html">Writing element content</a></dt><dt><a href="ar01s07.html">Writing Attribute</a></dt><dt><a href="ar01s08.html">Retrieving Attributes</a></dt><dt><a href="ar01s09.html">Encoding Conversion</a></dt><dt>A. <a href="apa.html">Compilation</a></dt><dt>B. <a href="apb.html">Sample Document</a></dt><dt>C. <a href="apc.html">Code for Keyword Example</a></dt><dt>D. <a href="apd.html">Code for XPath Example</a></dt><dt>E. <a href="ape.html">Code for Add Keyword Example</a></dt><dt>F. <a href="apf.html">Code for Add Attribute Example</a></dt><dt>G. <a href="apg.html">Code for Retrieving Attribute Value Example</a></dt><dt>H. <a href="aph.html">Code for Encoding Conversion Example</a></dt><dt>I. <a href="api.html">Acknowledgements</a></dt></dl></div><div class="abstract"><p class="title"><b>Abstract</b></p><p>Libxml is a freely licensed C language library for handling
<span class="acronym">XML</span>, portable across a large number of platforms. This
tutorial provides examples of its basic functions.</p></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="introduction"></a>Introduction</h2></div></div><div></div></div><p>Libxml is a C language library implementing functions for reading,
creating and manipulating <span class="acronym">XML</span> data. This tutorial

View File

@ -1,2 +1 @@
<!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>Index</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="aph.html" title="H. 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">Index</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="aph.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> </td></tr></table><hr></div><div class="index"><div class="titlepage"><div><div><h2 class="title"><a name="id2882914"></a>Index</h2></div></div><div></div></div><div class="index"><div class="indexdiv"><h3>A</h3><dl><dt>attribute</dt><dd><dl><dt>retrieving value, <a href="ar01s07.html">Retrieving Attributes</a></dt><dt>writing, <a href="ar01s06.html">Writing Attribute</a></dt></dl></dd></dl></div><div class="indexdiv"><h3>C</h3><dl><dt>compiler flags, <a href="apa.html">Compilation</a></dt></dl></div><div class="indexdiv"><h3>E</h3><dl><dt>element</dt><dd><dl><dt>retrieving content, <a href="ar01s04.html">Retrieving Element Content</a></dt><dt>writing content, <a href="ar01s05.html">Writing element content</a></dt></dl></dd><dt>encoding, <a href="ar01s03.html">Parsing the file</a>, <a href="ar01s08.html">Encoding Conversion</a></dt></dl></div><div class="indexdiv"><h3>F</h3><dl><dt>file</dt><dd><dl><dt>parsing, <a href="ar01s03.html">Parsing the file</a>-<a href="ar01s03.html">Parsing the file</a></dt><dt>saving, <a href="ar01s05.html">Writing element content</a></dt></dl></dd></dl></div><div class="indexdiv"><h3>X</h3><dl><dt>xmlChar, <a href="ar01s02.html">Data Types</a></dt><dt>xmlDoc, <a href="ar01s02.html">Data Types</a></dt><dt>xmlNodePtr, <a href="ar01s02.html">Data Types</a></dt></dl></div></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="aph.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">H. Acknowledgements </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>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Index</title><meta name="generator" content="DocBook XSL Stylesheets V1.60.1"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="api.html" title="I. 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">Index</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="api.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> </td></tr></table><hr></div><div class="index"><div class="titlepage"><div><div><h2 class="title"><a name="id2883028"></a>Index</h2></div></div><div></div></div><div class="index"><div class="indexdiv"><h3>A</h3><dl><dt>attribute</dt><dd><dl><dt>retrieving value, <a href="ar01s08.html">Retrieving Attributes</a></dt><dt>writing, <a href="ar01s07.html">Writing Attribute</a></dt></dl></dd></dl></div><div class="indexdiv"><h3>C</h3><dl><dt>compiler flags, <a href="apa.html">Compilation</a></dt></dl></div><div class="indexdiv"><h3>E</h3><dl><dt>element</dt><dd><dl><dt>retrieving content, <a href="ar01s04.html">Retrieving Element Content</a></dt><dt>writing content, <a href="ar01s06.html">Writing element content</a></dt></dl></dd><dt>encoding, <a href="ar01s03.html">Parsing the file</a>, <a href="ar01s09.html">Encoding Conversion</a></dt></dl></div><div class="indexdiv"><h3>F</h3><dl><dt>file</dt><dd><dl><dt>parsing, <a href="ar01s03.html">Parsing the file</a>-<a href="ar01s03.html">Parsing the file</a></dt><dt>saving, <a href="ar01s06.html">Writing element content</a></dt></dl></dd></dl></div><div class="indexdiv"><h3>X</h3><dl><dt>xmlChar, <a href="ar01s02.html">Data Types</a></dt><dt>xmlDoc, <a href="ar01s02.html">Data Types</a></dt><dt>xmlNodePtr, <a href="ar01s02.html">Data Types</a></dt></dl></div></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="api.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">I. Acknowledgements </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>

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,7 @@
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
<!ENTITY KEYWORD SYSTEM "includekeyword.c">
<!ENTITY XPATH SYSTEM "includexpath.c">
<!ENTITY STORY SYSTEM "includestory.xml">
<!ENTITY ADDKEYWORD SYSTEM "includeaddkeyword.c">
<!ENTITY ADDATTRIBUTE SYSTEM "includeaddattribute.c">
@ -56,6 +57,11 @@
<date>April 25, 2003</date>
<revremark>add compilation appendix</revremark>
</revision>
<revision>
<revnumber>8</revnumber>
<date>July 24, 2003</date>
<revremark>add XPath example</revremark>
</revision>
</revhistory>
</articleinfo>
<abstract>
@ -321,20 +327,106 @@ parseStory (xmlDocPtr doc, xmlNodePtr cur) {
</para>
</sect1>
<sect1 id="xmltutorialxpath">
<title>Using XPath to Retrieve Element Content</title>
<para>In addition to walking the document tree to find an element,
<application>Libxml2</application> includes support for
use of <application>XPath</application> expressions to retrieve sets of
nodes that match a specified criteria. Full documentation of the
<application>XPath</application> <acronym>API</acronym> is <ulink
url="http://xmlsoft.org/html/libxml-xpath.html">here</ulink>.
</para>
<para><application>XPath</application> allows searching through a document
for nodes that match specified criteria. In the example below we search
through a document for the contents of all <varname>keyword</varname>
elements.
<note>
<para>A full discussion of <application>XPath</application> is beyond
the scope of this document. For details on its use, see the <ulink
url="http://www.w3.org/TR/xpath">XPath specification</ulink>.</para>
</note>
Full code for this example is at <xref linkend="xpathappendix" />.
</para>
<para>Using <application>XPath</application> requires setting up an
xmlXPathContext and then supplying the <application>XPath</application>
expression and the context to the
<function>xmlXPathEvalExpression</function> function. The function returns
an xmlXPathObjectPtr, which includes the set of nodes satisfying the
<application>XPath</application> expression.</para>
<para>
<programlisting>
xmlXPathObjectPtr
getnodeset (xmlDocPtr doc, xmlChar *xpath){
<co id="cocontext" />xmlXPathContextPtr context;
xmlXPathObjectPtr result;
<co id="cocreatecontext" />context = xmlXPathNewContext(doc);
<co id="corunxpath" />result = xmlXPathEvalExpression(xpath, context);
<co id="cocheckxpathresult" />if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
printf("No result\n");
return NULL;
}
xmlXPathFreeContext(context);
return result;
</programlisting>
<calloutlist>
<callout arearefs="cocontext">
<para>First we declare our variables.</para>
</callout>
<callout arearefs="cocreatecontext">
<para>Initialize the <varname>context</varname> variable.</para>
</callout>
<callout arearefs="corunxpath">
<para>Apply the <application>XPath</application> expression.</para>
</callout>
<callout arearefs="cocheckxpathresult">
<para>Check the result.</para>
</callout>
</calloutlist>
</para>
<para>The xmlPathObjectPtr returned by the function contains a set of nodes
and other information needed to iterate through the set and act on the
results. For this example, our functions returns the
<varname>xmlXPathObjectPtr</varname>. We use it to print the contents of
<varname>keyword</varname> nodes in our document. The node set object
includes the number of elements in the set (<varname>nodeNr</varname>) and
an array of nodes (<varname>nodeTab</varname>):
<programlisting>
<co id="conodesetcounter" />for (i=0; i &lt; nodeset->nodeNr; i++) {
<co id="coprintkeywords" />keyword = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1);
printf("keyword: %s\n", keyword);
}
</programlisting>
<calloutlist>
<callout arearefs="conodesetcounter">
<para>The value of <varname>nodeset->Nr</varname> holds the number of
elements in the node set. Here we use it to iterate through the array.</para>
</callout>
<callout arearefs="coprintkeywords">
<para>Here we print the contents of each of the nodes returned.
<note>
<para>Note that we are printing the child node of the node that is
returned, because the contents of the <varname>keyword</varname>
element are a child text node.</para>
</note>
</para>
</callout>
</calloutlist>
</para>
</sect1>
<sect1 id="xmltutorialwritingcontent">
<title>Writing element content</title>
<para><indexterm>
<primary>element</primary>
<secondary>writing content</secondary>
</indexterm>
Writing element content uses many of the same steps we used above
&mdash; 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
Writing element content uses many of the same steps we used above
&mdash; 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:
<xref linkend="addkeywordappendix" /></para>
<xref linkend="addkeywordappendix" /></para>
<para>
The main difference in this example is in
<function>parseStory</function>:
@ -597,6 +689,12 @@ Data encoding compatibility problems are one of the most common
<programlisting>&KEYWORD;</programlisting>
</para>
</appendix>
<appendix id="xpathappendix">
<title>Code for XPath Example</title>
<para>
<programlisting>&XPATH;</programlisting>
</para>
</appendix>
<appendix id="addkeywordappendix">
<title>Code for Add Keyword Example</title>
<para>
@ -631,6 +729,7 @@ Data encoding compatibility problems are one of the most common
<member>Christopher R. Harris</member>
<member>Igor Zlatkovic</member>
<member>Niraj Tolia</member>
<member>David Turover</member>
</simplelist>
</para>
</appendix>