• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<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.61.2"><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>
2      </p><pre class="programlisting">
3#include &lt;stdio.h&gt;
4#include &lt;string.h&gt;
5#include &lt;stdlib.h&gt;
6#include &lt;libxml/xmlmemory.h&gt;
7#include &lt;libxml/parser.h&gt;
8
9void
10parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword) {
11
12	xmlNewTextChild (cur, NULL, "keyword", keyword);
13    return;
14}
15
16xmlDocPtr
17parseDoc(char *docname, char *keyword) {
18
19	xmlDocPtr doc;
20	xmlNodePtr cur;
21
22	doc = xmlParseFile(docname);
23
24	if (doc == NULL ) {
25		fprintf(stderr,"Document not parsed successfully. \n");
26		return (NULL);
27	}
28
29	cur = xmlDocGetRootElement(doc);
30
31	if (cur == NULL) {
32		fprintf(stderr,"empty document\n");
33		xmlFreeDoc(doc);
34		return (NULL);
35	}
36
37	if (xmlStrcmp(cur-&gt;name, (const xmlChar *) "story")) {
38		fprintf(stderr,"document of the wrong type, root node != story");
39		xmlFreeDoc(doc);
40		return (NULL);
41	}
42
43	cur = cur-&gt;xmlChildrenNode;
44	while (cur != NULL) {
45		if ((!xmlStrcmp(cur-&gt;name, (const xmlChar *)"storyinfo"))){
46			parseStory (doc, cur, keyword);
47		}
48
49	cur = cur-&gt;next;
50	}
51	return(doc);
52}
53
54int
55main(int argc, char **argv) {
56
57	char *docname;
58	char *keyword;
59	xmlDocPtr doc;
60
61	if (argc &lt;= 2) {
62		printf("Usage: %s docname, keyword\n", argv[0]);
63		return(0);
64	}
65
66	docname = argv[1];
67	keyword = argv[2];
68	doc = parseDoc (docname, keyword);
69	if (doc != NULL) {
70		xmlSaveFormatFile (docname, doc, 0);
71		xmlFreeDoc(doc);
72	}
73
74	return (1);
75}
76
77</pre><p>
78    </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>
79