Blame doc/tutorial/ar01s03.html

Packit Service a31ea6
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Parsing the file</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="ar01s02.html" title="Data Types"><link rel="next" href="ar01s04.html" title="Retrieving Element Content"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">

Parsing the file

Packit Service a31ea6
Parsing the file requires only the name of the file and a single
Packit Service a31ea6
      function call, plus error checking. Full code: Appendix C, Code for Keyword Example

Packit Service a31ea6
    

Packit Service a31ea6
        1 xmlDocPtr doc;
Packit Service a31ea6
	2 xmlNodePtr cur;
Packit Service a31ea6
Packit Service a31ea6
	3 doc = xmlParseFile(docname);
Packit Service a31ea6
	
Packit Service a31ea6
	4 if (doc == NULL ) {
Packit Service a31ea6
		fprintf(stderr,"Document not parsed successfully. \n");
Packit Service a31ea6
		return;
Packit Service a31ea6
	}
Packit Service a31ea6
Packit Service a31ea6
	5 cur = xmlDocGetRootElement(doc);
Packit Service a31ea6
	
Packit Service a31ea6
	6 if (cur == NULL) {
Packit Service a31ea6
		fprintf(stderr,"empty document\n");
Packit Service a31ea6
		xmlFreeDoc(doc);
Packit Service a31ea6
		return;
Packit Service a31ea6
	}
Packit Service a31ea6
	
Packit Service a31ea6
	7 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;
Packit Service a31ea6
	}
Packit Service a31ea6
Packit Service a31ea6
    

Packit Service a31ea6
      

1

Declare the pointer that will point to your parsed document.

2

Declare a node pointer (you'll need this in order to

Packit Service a31ea6
	  interact with individual nodes).

4

Check to see that the document was successfully parsed. If it

Packit Service a31ea6
	    was not, libxml will at this point
Packit Service a31ea6
	    register an error and stop. 
Packit Service a31ea6
	    

[Note]Note

Packit Service a31ea6
One common example of an error at this point is improper
Packit Service a31ea6
	    handling of encoding. The XML standard requires
Packit Service a31ea6
	    documents stored with an encoding other than UTF-8 or UTF-16 to
Packit Service a31ea6
	    contain an explicit declaration of their encoding. If the
Packit Service a31ea6
	    declaration is there, libxml will
Packit Service a31ea6
	    automatically perform the necessary conversion to UTF-8 for
Packit Service a31ea6
		you. More information on XML's encoding
Packit Service a31ea6
		requirements is contained in the standard.

Packit Service a31ea6
	  

5

Retrieve the document's root element.

6

Check to make sure the document actually contains something.

7

In our case, we need to make sure the document is the right

Packit Service a31ea6
	  type. "story" is the root type of the documents used in this
Packit Service a31ea6
	  tutorial.

Packit Service a31ea6
      
Packit Service a31ea6
    

</body></html>