1<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.61.2"><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>
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
9
10xmlDocPtr
11parseDoc(char *docname, char *uri) {
12
13	xmlDocPtr doc;
14	xmlNodePtr cur;
15	xmlNodePtr newnode;
16	xmlAttrPtr newattr;
17
18	doc = xmlParseFile(docname);
19
20	if (doc == NULL ) {
21		fprintf(stderr,"Document not parsed successfully. \n");
22		return (NULL);
23	}
24
25	cur = xmlDocGetRootElement(doc);
26
27	if (cur == NULL) {
28		fprintf(stderr,"empty document\n");
29		xmlFreeDoc(doc);
30		return (NULL);
31	}
32
33	if (xmlStrcmp(cur-&gt;name, (const xmlChar *) "story")) {
34		fprintf(stderr,"document of the wrong type, root node != story");
35		xmlFreeDoc(doc);
36		return (NULL);
37	}
38
39	newnode = xmlNewTextChild (cur, NULL, "reference", NULL);
40	newattr = xmlNewProp (newnode, "uri", uri);
41	return(doc);
42}
43
44int
45main(int argc, char **argv) {
46
47	char *docname;
48	char *uri;
49	xmlDocPtr doc;
50
51	if (argc &lt;= 2) {
52		printf("Usage: %s docname, uri\n", argv[0]);
53		return(0);
54	}
55
56	docname = argv[1];
57	uri = argv[2];
58	doc = parseDoc (docname, uri);
59	if (doc != NULL) {
60		xmlSaveFormatFile (docname, doc, 1);
61		xmlFreeDoc(doc);
62	}
63	return (1);
64}
65
66</pre><p>
67    </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>
68