MST 2004 John Fleck | d14bccc | 2004-02-15 01:57:42 +0000 | [diff] [blame] | 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> |
MST 2003 John Fleck | 731967e | 2003-01-27 00:39:50 +0000 | [diff] [blame] | 2 | </p><pre class="programlisting"> |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 3 | #include <stdio.h> |
MST 2002 John Fleck | bd3b4fd | 2002-11-11 03:41:11 +0000 | [diff] [blame] | 4 | #include <string.h> |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 5 | #include <stdlib.h> |
| 6 | #include <libxml/xmlmemory.h> |
MST 2002 John Fleck | bd3b4fd | 2002-11-11 03:41:11 +0000 | [diff] [blame] | 7 | #include <libxml/parser.h> |
| 8 | |
| 9 | |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 10 | xmlDocPtr |
| 11 | parseDoc(char *docname, char *uri) { |
MST 2002 John Fleck | bd3b4fd | 2002-11-11 03:41:11 +0000 | [diff] [blame] | 12 | |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 13 | xmlDocPtr doc; |
| 14 | xmlNodePtr cur; |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 15 | xmlNodePtr newnode; |
| 16 | xmlAttrPtr newattr; |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 17 | |
| 18 | doc = xmlParseFile(docname); |
| 19 | |
| 20 | if (doc == NULL ) { |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 21 | fprintf(stderr,"Document not parsed successfully. \n"); |
| 22 | return (NULL); |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | cur = xmlDocGetRootElement(doc); |
| 26 | |
| 27 | if (cur == NULL) { |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 28 | fprintf(stderr,"empty document\n"); |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 29 | xmlFreeDoc(doc); |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 30 | return (NULL); |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 31 | } |
| 32 | |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 33 | if (xmlStrcmp(cur->name, (const xmlChar *) "story")) { |
| 34 | fprintf(stderr,"document of the wrong type, root node != story"); |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 35 | xmlFreeDoc(doc); |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 36 | return (NULL); |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 37 | } |
| 38 | |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 39 | newnode = xmlNewTextChild (cur, NULL, "reference", NULL); |
| 40 | newattr = xmlNewProp (newnode, "uri", uri); |
| 41 | return(doc); |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 42 | } |
MST 2002 John Fleck | bd3b4fd | 2002-11-11 03:41:11 +0000 | [diff] [blame] | 43 | |
| 44 | int |
| 45 | main(int argc, char **argv) { |
| 46 | |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 47 | char *docname; |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 48 | char *uri; |
| 49 | xmlDocPtr doc; |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 50 | |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 51 | if (argc <= 2) { |
| 52 | printf("Usage: %s docname, uri\n", argv[0]); |
MST 2002 John Fleck | bd3b4fd | 2002-11-11 03:41:11 +0000 | [diff] [blame] | 53 | return(0); |
| 54 | } |
| 55 | |
MDT 2003 John Fleck | 8aff3b7 | 2003-04-26 03:54:07 +0000 | [diff] [blame] | 56 | docname = argv[1]; |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 57 | uri = argv[2]; |
| 58 | doc = parseDoc (docname, uri); |
| 59 | if (doc != NULL) { |
| 60 | xmlSaveFormatFile (docname, doc, 1); |
| 61 | xmlFreeDoc(doc); |
| 62 | } |
MST 2002 John Fleck | bd3b4fd | 2002-11-11 03:41:11 +0000 | [diff] [blame] | 63 | return (1); |
| 64 | } |
| 65 | |
MST 2003 John Fleck | 731967e | 2003-01-27 00:39:50 +0000 | [diff] [blame] | 66 | </pre><p> |
MDT 2003 John Fleck | 63f3a47 | 2003-07-24 21:48:30 +0000 | [diff] [blame] | 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> |