Blame doc/tutorial/apf.html

Packit Service a31ea6
<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">

F. Code for Add Attribute Example

Packit Service a31ea6
      

Packit Service a31ea6
#include <stdio.h>
Packit Service a31ea6
#include <string.h>
Packit Service a31ea6
#include <stdlib.h>
Packit Service a31ea6
#include <libxml/xmlmemory.h>
Packit Service a31ea6
#include <libxml/parser.h>
Packit Service a31ea6
Packit Service a31ea6
Packit Service a31ea6
xmlDocPtr
Packit Service a31ea6
parseDoc(char *docname, char *uri) {
Packit Service a31ea6
Packit Service a31ea6
	xmlDocPtr doc;
Packit Service a31ea6
	xmlNodePtr cur;
Packit Service a31ea6
	xmlNodePtr newnode;
Packit Service a31ea6
	xmlAttrPtr newattr;
Packit Service a31ea6
Packit Service a31ea6
	doc = xmlParseFile(docname);
Packit Service a31ea6
	
Packit Service a31ea6
	if (doc == NULL ) {
Packit Service a31ea6
		fprintf(stderr,"Document not parsed successfully. \n");
Packit Service a31ea6
		return (NULL);
Packit Service a31ea6
	}
Packit Service a31ea6
	
Packit Service a31ea6
	cur = xmlDocGetRootElement(doc);
Packit Service a31ea6
	
Packit Service a31ea6
	if (cur == NULL) {
Packit Service a31ea6
		fprintf(stderr,"empty document\n");
Packit Service a31ea6
		xmlFreeDoc(doc);
Packit Service a31ea6
		return (NULL);
Packit Service a31ea6
	}
Packit Service a31ea6
	
Packit Service a31ea6
	if (xmlStrcmp(cur->name, (const xmlChar *) "story")) {
Packit Service a31ea6
		fprintf(stderr,"document of the wrong type, root node != story");
Packit Service a31ea6
		xmlFreeDoc(doc);
Packit Service a31ea6
		return (NULL);
Packit Service a31ea6
	}
Packit Service a31ea6
	
Packit Service a31ea6
	newnode = xmlNewTextChild (cur, NULL, "reference", NULL);
Packit Service a31ea6
	newattr = xmlNewProp (newnode, "uri", uri);
Packit Service a31ea6
	return(doc);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6
int
Packit Service a31ea6
main(int argc, char **argv) {
Packit Service a31ea6
Packit Service a31ea6
	char *docname;
Packit Service a31ea6
	char *uri;
Packit Service a31ea6
	xmlDocPtr doc;
Packit Service a31ea6
Packit Service a31ea6
	if (argc <= 2) {
Packit Service a31ea6
		printf("Usage: %s docname, uri\n", argv[0]);
Packit Service a31ea6
		return(0);
Packit Service a31ea6
	}
Packit Service a31ea6
Packit Service a31ea6
	docname = argv[1];
Packit Service a31ea6
	uri = argv[2];
Packit Service a31ea6
	doc = parseDoc (docname, uri);
Packit Service a31ea6
	if (doc != NULL) {
Packit Service a31ea6
		xmlSaveFormatFile (docname, doc, 1);
Packit Service a31ea6
		xmlFreeDoc(doc);
Packit Service a31ea6
	}
Packit Service a31ea6
	return (1);
Packit Service a31ea6
}
Packit Service a31ea6
Packit Service a31ea6

Packit Service a31ea6
    

</body></html>