MDT 2002 John Fleck | 598f6eb | 2002-06-04 15:10:36 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
MST 2003 John Fleck | 731967e | 2003-01-27 00:39:50 +0000 | [diff] [blame^] | 2 | <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 Fleck | 598f6eb | 2002-06-04 15:10:36 +0000 | [diff] [blame] | 4 | #include <stdio.h> |
| 5 | #include <string.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <libxml/xmlmemory.h> |
| 8 | #include <libxml/parser.h> |
| 9 | |
| 10 | |
| 11 | xmlDocPtr |
| 12 | parseDoc(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 | |
| 45 | int |
| 46 | main(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); |
MDT 2002 John Fleck | 77e4d35 | 2002-09-01 01:37:11 +0000 | [diff] [blame] | 62 | xmlFreeDoc(doc); |
MDT 2002 John Fleck | 598f6eb | 2002-06-04 15:10:36 +0000 | [diff] [blame] | 63 | } |
| 64 | return (1); |
| 65 | } |
| 66 | |
MST 2003 John Fleck | 731967e | 2003-01-27 00:39:50 +0000 | [diff] [blame^] | 67 | </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> |