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 <stdio.h> 5#include <string.h> 6#include <stdlib.h> 7#include <libxml/xmlmemory.h> 8#include <libxml/parser.h> 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,"Document not parsed successfully. \n"); 23 return (NULL); 24 } 25 26 cur = xmlDocGetRootElement(doc); 27 28 if (cur == NULL) { 29 fprintf(stderr,"empty document\n"); 30 xmlFreeDoc(doc); 31 return (NULL); 32 } 33 34 if (xmlStrcmp(cur->name, (const xmlChar *) "story")) { 35 fprintf(stderr,"document of the wrong type, root node != story"); 36 xmlFreeDoc(doc); 37 return (NULL); 38 } 39 40 newnode = xmlNewTextChild (cur, NULL, "reference", NULL); 41 newattr = xmlNewProp (newnode, "uri", 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 <= 2) { 53 printf("Usage: %s docname, uri\n", 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