aboutsummaryrefslogtreecommitdiff
path: root/libxml2-2.9.10/doc/tutorial/includeaddattribute.c
blob: bb6a18b478f50e7d69fb8f0e17f8f670153ae347 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<![CDATA[
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>


xmlDocPtr
parseDoc(char *docname, char *uri) {

	xmlDocPtr doc;
	xmlNodePtr cur;
	xmlNodePtr newnode;
	xmlAttrPtr newattr;

	doc = xmlParseFile(docname);
	
	if (doc == NULL ) {
		fprintf(stderr,"Document not parsed successfully. \n");
		return (NULL);
	}
	
	cur = xmlDocGetRootElement(doc);
	
	if (cur == NULL) {
		fprintf(stderr,"empty document\n");
		xmlFreeDoc(doc);
		return (NULL);
	}
	
	if (xmlStrcmp(cur->name, (const xmlChar *) "story")) {
		fprintf(stderr,"document of the wrong type, root node != story");
		xmlFreeDoc(doc);
		return (NULL);
	}
	
	newnode = xmlNewTextChild (cur, NULL, "reference", NULL);
	newattr = xmlNewProp (newnode, "uri", uri);
	return(doc);
}

int
main(int argc, char **argv) {

	char *docname;
	char *uri;
	xmlDocPtr doc;

	if (argc <= 2) {
		printf("Usage: %s docname, uri\n", argv[0]);
		return(0);
	}

	docname = argv[1];
	uri = argv[2];
	doc = parseDoc (docname, uri);
	if (doc != NULL) {
		xmlSaveFormatFile (docname, doc, 1);
		xmlFreeDoc(doc);
	}
	return (1);
}
]]>