blob: 28d0ea04b2ecce60ad1f1b0085bc29f99d4b97ff [file] [log] [blame]
MDT 2002 John Fleck54520832002-06-13 03:30:26 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +00002<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>
MST 2003 John Fleck731967e2003-01-27 00:39:50 +00003 </p><pre class="programlisting">
MDT 2002 John Fleck54520832002-06-13 03:30:26 +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
MDT 2002 John Fleck54520832002-06-13 03:30:26 +000010
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +000011xmlDocPtr
12parseDoc(char *docname, char *uri) {
MDT 2002 John Fleck54520832002-06-13 03:30:26 +000013
14 xmlDocPtr doc;
15 xmlNodePtr cur;
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +000016 xmlNodePtr newnode;
17 xmlAttrPtr newattr;
MDT 2002 John Fleck54520832002-06-13 03:30:26 +000018
19 doc = xmlParseFile(docname);
20
21 if (doc == NULL ) {
22 fprintf(stderr,&quot;Document not parsed successfully. \n&quot;);
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +000023 return (NULL);
MDT 2002 John Fleck54520832002-06-13 03:30:26 +000024 }
25
26 cur = xmlDocGetRootElement(doc);
27
28 if (cur == NULL) {
29 fprintf(stderr,&quot;empty document\n&quot;);
30 xmlFreeDoc(doc);
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +000031 return (NULL);
MDT 2002 John Fleck54520832002-06-13 03:30:26 +000032 }
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);
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +000037 return (NULL);
MDT 2002 John Fleck54520832002-06-13 03:30:26 +000038 }
39
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +000040 newnode = xmlNewTextChild (cur, NULL, &quot;reference&quot;, NULL);
41 newattr = xmlNewProp (newnode, &quot;uri&quot;, uri);
42 return(doc);
MDT 2002 John Fleck54520832002-06-13 03:30:26 +000043}
44
45int
46main(int argc, char **argv) {
47
48 char *docname;
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +000049 char *uri;
50 xmlDocPtr doc;
MDT 2002 John Fleck54520832002-06-13 03:30:26 +000051
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +000052 if (argc &lt;= 2) {
53 printf(&quot;Usage: %s docname, uri\n&quot;, argv[0]);
MDT 2002 John Fleck54520832002-06-13 03:30:26 +000054 return(0);
55 }
56
57 docname = argv[1];
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +000058 uri = argv[2];
59 doc = parseDoc (docname, uri);
60 if (doc != NULL) {
61 xmlSaveFormatFile (docname, doc, 1);
62 xmlFreeDoc(doc);
63 }
MDT 2002 John Fleck54520832002-06-13 03:30:26 +000064 return (1);
65}
66
MST 2003 John Fleck731967e2003-01-27 00:39:50 +000067</pre><p>
MDT 2003 John Fleck8aff3b72003-04-26 03:54:07 +000068 </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>