blob: bc2359bdcd7ece0003e14ebae3f80ec6a14e120f [file] [log] [blame]
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
MST 2003 John Fleck731967e2003-01-27 00:39:50 +00002<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>D. Code for Add Attribute Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.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 Add Keyword Example"><link rel="next" href="ape.html" title="E. Code for Retrieving Attribute Value Example"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">D. Code for Add Attribute Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apc.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ape.html">Next</a></td></tr></table><hr></div><div class="appendix" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="addattributeappendix"></a>D. Code for Add Attribute Example</h2></div></div><div></div></div><p>
3 </p><pre class="programlisting">
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +00004#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);
MDT 2002 John Fleck77e4d352002-09-01 01:37:11 +000062 xmlFreeDoc(doc);
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +000063 }
64 return (1);
65}
66
MST 2003 John Fleck731967e2003-01-27 00:39:50 +000067</pre><p>
68 </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="apc.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ape.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">C. Code for Add Keyword Example </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> E. Code for Retrieving Attribute Value Example</td></tr></table></div></body></html>