From 54520837add57527ec0c0ccc34d37cc1e3c51aa2 Mon Sep 17 00:00:00 2001 From: MDT 2002 John Fleck Date: Thu, 13 Jun 2002 03:30:26 +0000 Subject: [PATCH] doc/tutorial/xmltutorial.xml doc/tutorial/ar01s07.html Wed Jun 12 21:26:08 MDT 2002 John Fleck * doc/tutorial/xmltutorial.xml * doc/tutorial/ar01s07.html * doc/tutorial/ape.html * doc/tutorial/includegetattribute.c adding section to tutorial about retrieving an attribute value --- ChangeLog | 9 ++++ doc/tutorial/apa.html | 4 +- doc/tutorial/apd.html | 4 +- doc/tutorial/ape.html | 73 ++++++++++++++++++++++++++++++ doc/tutorial/ar01s06.html | 4 +- doc/tutorial/ar01s07.html | 35 ++++++++++++++ doc/tutorial/includegetattribute.c | 69 ++++++++++++++++++++++++++++ doc/tutorial/index.html | 5 +- doc/tutorial/xmltutorial.xml | 72 ++++++++++++++++++++++++----- 9 files changed, 254 insertions(+), 21 deletions(-) create mode 100644 doc/tutorial/ape.html create mode 100644 doc/tutorial/ar01s07.html create mode 100644 doc/tutorial/includegetattribute.c diff --git a/ChangeLog b/ChangeLog index fe516b07..69ce2d0c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Wed Jun 12 21:26:08 MDT 2002 John Fleck + + * doc/tutorial/xmltutorial.xml + * doc/tutorial/ar01s07.html + * doc/tutorial/ape.html + * doc/tutorial/includegetattribute.c + adding section to tutorial about retrieving an attribute + value + Tue Jun 11 12:07:04 CEST 2002 Daniel Veillard * parser.c: applied a couple of patches from Peter Jacobi to start diff --git a/doc/tutorial/apa.html b/doc/tutorial/apa.html index d01ea2ed..375fb695 100644 --- a/doc/tutorial/apa.html +++ b/doc/tutorial/apa.html @@ -1,5 +1,5 @@ -A. Sample Document

A. Sample Document

+A. Sample Document

A. Sample Document

 <?xml version="1.0"?>
 <story>
   <storyinfo>
@@ -12,4 +12,4 @@
     <para>This is the body text.</para>
   </body>
 </story>
-
+
diff --git a/doc/tutorial/apd.html b/doc/tutorial/apd.html index 66cddfe3..5e56f3c5 100644 --- a/doc/tutorial/apd.html +++ b/doc/tutorial/apd.html @@ -1,5 +1,5 @@ -D. Code for Add Attribute Example

D. Code for Add Attribute Example

+D. Code for Add Attribute Example

D. Code for Add Attribute Example

 #include <stdio.h>
 #include <string.h>
@@ -64,4 +64,4 @@ main(int argc, char **argv) {
 }
 
 
-

+

diff --git a/doc/tutorial/ape.html b/doc/tutorial/ape.html new file mode 100644 index 00000000..9be15e85 --- /dev/null +++ b/doc/tutorial/ape.html @@ -0,0 +1,73 @@ + +E. Code for Retrieving Attribute Value Example

E. Code for Retrieving Attribute Value Example

+

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

diff --git a/doc/tutorial/ar01s06.html b/doc/tutorial/ar01s06.html index 8d2afd15..2bbd2dfa 100644 --- a/doc/tutorial/ar01s06.html +++ b/doc/tutorial/ar01s06.html @@ -1,5 +1,5 @@ -Writing Attribute

Writing Attribute

Writing an attribute is similar to writing text to a new element. In +Writing Attribute

Writing Attribute

Writing an attribute is similar to writing text to a new element. In this case, we'll add a reference URI to our document. Full code:Appendix D.

A reference is a child of the story @@ -27,4 +27,4 @@

1

First we add a new node at the location of the current node pointer, cur. using the xmlNewTextChild function.

Once the node is added, the file is written to disk just as in the - previous example in which we added an element with text content.

+ previous example in which we added an element with text content.

diff --git a/doc/tutorial/ar01s07.html b/doc/tutorial/ar01s07.html new file mode 100644 index 00000000..7dda324c --- /dev/null +++ b/doc/tutorial/ar01s07.html @@ -0,0 +1,35 @@ + +Retrieving Attributes

Retrieving Attributes

Retrieving the value of an attribute is similar to the previous + example in which we retrieved a node's text contents. In this case we'll + extract the value of the URI we added in the previous + section. Full code: Appendix E.

+ The initial steps for this example are similar to the previous ones: parse + the doc, find the element you are interested in, then enter a function to + carry out the specific task required. In this case, we call + getReference: +

+void
+getReference (xmlDocPtr doc, xmlNodePtr cur) {
+
+	cur = cur->xmlChildrenNode;
+	while (cur != NULL) {
+	    if ((!xmlStrcmp(cur->name, (const xmlChar *)"reference"))) {
+	1 printf("uri: %s\n", xmlGetProp(cur, "uri"));
+		    }
+	    cur = cur->next;
+	}
+    return;
+}
+      
+ +
1

+ The key function is xmlGetProp, which returns an + xmlChar containing the attribute's value. In this case, + we just print it out. +

Note

+ If you are using a DTD that declares a fixed or + default value for the attribute, this function will retrieve it. +

+

+ +

diff --git a/doc/tutorial/includegetattribute.c b/doc/tutorial/includegetattribute.c new file mode 100644 index 00000000..f934667e --- /dev/null +++ b/doc/tutorial/includegetattribute.c @@ -0,0 +1,69 @@ + +#include +#include +#include +#include + +void +getReference (xmlDocPtr doc, xmlNodePtr cur) { + + cur = cur->xmlChildrenNode; + while (cur != NULL) { + if ((!xmlStrcmp(cur->name, (const xmlChar *)"reference"))) { + printf("uri: %s\n", xmlGetProp(cur, "uri")); + } + cur = cur->next; + } + return; +} + + +void +parseDoc(char *docname) { + + xmlDocPtr doc; + xmlNodePtr cur; + + doc = xmlParseFile(docname); + + if (doc == NULL ) { + fprintf(stderr,"Document not parsed successfully. \n"); + return; + } + + cur = xmlDocGetRootElement(doc); + + if (cur == NULL) { + fprintf(stderr,"empty document\n"); + xmlFreeDoc(doc); + return; + } + + if (xmlStrcmp(cur->name, (const xmlChar *) "story")) { + fprintf(stderr,"document of the wrong type, root node != story"); + xmlFreeDoc(doc); + return; + } + + getReference (doc, cur); + + return; +} + +int +main(int argc, char **argv) { + + char *docname; + + if (argc <= 1) { + printf("Usage: %s docname\n", argv[0]); + return(0); + } + + docname = argv[1]; + parseDoc (docname); + + return (1); +} +]]> diff --git a/doc/tutorial/index.html b/doc/tutorial/index.html index eef00e37..3dee1480 100644 --- a/doc/tutorial/index.html +++ b/doc/tutorial/index.html @@ -1,5 +1,5 @@ -Libxml Tutorial

Libxml Tutorial

John Fleck

Revision History
Revision 1June 4,2002

Abstract

Libxml is a freely licensed C language library for handling +Libxml Tutorial

Libxml Tutorial

John Fleck

Revision History
Revision 1June 4,2002
Revision 2June 12, 2002

Abstract

Libxml is a freely licensed C language library for handling XML, portable across a large number of platforms. This tutorial provides examples of its basic functions.

Introduction

Libxml is a C language library implementing functions for reading, creating and manipulating XML data. This tutorial @@ -11,6 +11,5 @@

The tutorial is based on a simple XML application I use for articles I write. The format includes metadata and the body of the article.

The example code in this tutorial demonstrates how to: -

  • Parse the document.

  • Extract the text within a specified element.

  • Add an element and its content.

  • Extract the value of an attribute.

  • Add an attribute.

- +
  • Parse the document.

  • Extract the text within a specified element.

  • Add an element and its content.

  • Add an attribute.

  • Extract the value of an attribute.

Full code for the examples is included in the appendices.

diff --git a/doc/tutorial/xmltutorial.xml b/doc/tutorial/xmltutorial.xml index be9a1ba3..e73ec3a4 100644 --- a/doc/tutorial/xmltutorial.xml +++ b/doc/tutorial/xmltutorial.xml @@ -5,6 +5,7 @@ + ]>
@@ -22,6 +23,10 @@ 1 June 4,2002 + + 2 + June 12, 2002 + @@ -58,14 +63,13 @@ Add an element and its content. + + Add an attribute. + Extract the value of an attribute. - - Add an attribute. - - Full code for the examples is included in the appendices. @@ -288,15 +292,7 @@ parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword) { the old file. The second parameter is a pointer to the xmlDoc structure. Setting the third parameter equal to one ensures indenting on output. - - - Writing Attribute @@ -341,6 +337,52 @@ parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword) { + + Retrieving Attributes + Retrieving the value of an attribute is similar to the previous + example in which we retrieved a node's text contents. In this case we'll + extract the value of the URI we added in the previous + section. Full code: . + + The initial steps for this example are similar to the previous ones: parse + the doc, find the element you are interested in, then enter a function to + carry out the specific task required. In this case, we call + getReference: + +void +getReference (xmlDocPtr doc, xmlNodePtr cur) { + + cur = cur->xmlChildrenNode; + while (cur != NULL) { + if ((!xmlStrcmp(cur->name, (const xmlChar *)"reference"))) { + printf("uri: %s\n", xmlGetProp(cur, "uri")); + } + cur = cur->next; + } + return; +} + + + + + + The key function is xmlGetProp, which returns an + xmlChar containing the attribute's value. In this case, + we just print it out. + + + If you are using a DTD that declares a fixed or + default value for the attribute, this function will retrieve it. + + + + + + + + +