apd.html revision 598f6ebb94b00e6583f4b330dcad08ab3a0c5a36
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>D. Code for Add Attribute Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="apc.html" title="C. Code for Add Keyword Example"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">D. Code for Add Attribute Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apc.html">Prev</a>�</td><th width="60%" align="center">�</th><td width="20%" align="right">�</td></tr></table><hr></div><div class="appendix"><h2 class="title" style="clear: both"><a name="addattributeappendix"></a>D. Code for Add Attribute Example</h2><p>
3      <pre class="programlisting">
4#include &lt;stdio.h&gt;
5#include &lt;string.h&gt;
6#include &lt;stdlib.h&gt;
7#include &lt;libxml/xmlmemory.h&gt;
8#include &lt;libxml/parser.h&gt;
9
10
11xmlDocPtr
12parseDoc(char *docname, char *uri) {
13
14	xmlDocPtr doc;
15	xmlNodePtr cur;
16	xmlNodePtr newnode;
17	xmlAttrPtr newattr;
18
19	doc = xmlParseFile(docname);
20	
21	if (doc == NULL ) {
22		fprintf(stderr,&quot;Document not parsed successfully. \n&quot;);
23		return (NULL);
24	}
25	
26	cur = xmlDocGetRootElement(doc);
27	
28	if (cur == NULL) {
29		fprintf(stderr,&quot;empty document\n&quot;);
30		xmlFreeDoc(doc);
31		return (NULL);
32	}
33	
34	if (xmlStrcmp(cur-&gt;name, (const xmlChar *) &quot;story&quot;)) {
35		fprintf(stderr,&quot;document of the wrong type, root node != story&quot;);
36		xmlFreeDoc(doc);
37		return (NULL);
38	}
39	
40	newnode = xmlNewTextChild (cur, NULL, &quot;reference&quot;, NULL);
41	newattr = xmlNewProp (newnode, &quot;uri&quot;, uri);
42	return(doc);
43}
44
45int
46main(int argc, char **argv) {
47
48	char *docname;
49	char *uri;
50	xmlDocPtr doc;
51
52	if (argc &lt;= 2) {
53		printf(&quot;Usage: %s docname, uri\n&quot;, argv[0]);
54		return(0);
55	}
56
57	docname = argv[1];
58	uri = argv[2];
59	doc = parseDoc (docname, uri);
60	if (doc != NULL) {
61		xmlSaveFormatFile (docname, doc, 1);
62	}
63	return (1);
64}
65
66</pre>
67    </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="apc.html">Prev</a>�</td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right">�</td></tr><tr><td width="40%" align="left" valign="top">C. Code for Add Keyword Example�</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">�</td></tr></table></div></body></html>
68