includeaddattribute.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <![CDATA[
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <libxml/xmlmemory.h>
  6. #include <libxml/parser.h>
  7. xmlDocPtr
  8. parseDoc(char *docname, char *uri) {
  9. xmlDocPtr doc;
  10. xmlNodePtr cur;
  11. xmlNodePtr newnode;
  12. xmlAttrPtr newattr;
  13. doc = xmlParseFile(docname);
  14. if (doc == NULL ) {
  15. fprintf(stderr,"Document not parsed successfully. \n");
  16. return (NULL);
  17. }
  18. cur = xmlDocGetRootElement(doc);
  19. if (cur == NULL) {
  20. fprintf(stderr,"empty document\n");
  21. xmlFreeDoc(doc);
  22. return (NULL);
  23. }
  24. if (xmlStrcmp(cur->name, (const xmlChar *) "story")) {
  25. fprintf(stderr,"document of the wrong type, root node != story");
  26. xmlFreeDoc(doc);
  27. return (NULL);
  28. }
  29. newnode = xmlNewTextChild (cur, NULL, "reference", NULL);
  30. newattr = xmlNewProp (newnode, "uri", uri);
  31. return(doc);
  32. }
  33. int
  34. main(int argc, char **argv) {
  35. char *docname;
  36. char *uri;
  37. xmlDocPtr doc;
  38. if (argc <= 2) {
  39. printf("Usage: %s docname, uri\n", argv[0]);
  40. return(0);
  41. }
  42. docname = argv[1];
  43. uri = argv[2];
  44. doc = parseDoc (docname, uri);
  45. if (doc != NULL) {
  46. xmlSaveFormatFile (docname, doc, 1);
  47. xmlFreeDoc(doc);
  48. }
  49. return (1);
  50. }
  51. ]]>