Back to Turicreate

Parsing the file

deps/src/libxml2-2.9.1/doc/tutorial/ar01s03.html

6.4.12.1 KB
Original Source
Parsing the file
Prev

Parsing the file

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

![1](images/callouts/1.png)xmlDocPtr doc; ![2](images/callouts/2.png)xmlNodePtr cur; ![3](images/callouts/3.png)doc = xmlParseFile(docname); ![4](images/callouts/4.png)if (doc == NULL ) {
		fprintf(stderr,"Document not parsed successfully. \n");
		return;
	} ![5](images/callouts/5.png)cur = xmlDocGetRootElement(doc); ![6](images/callouts/6.png)if (cur == NULL) {
		fprintf(stderr,"empty document\n");
		xmlFreeDoc(doc);
		return;
	} ![7](images/callouts/7.png)if (xmlStrcmp(cur->name, (const xmlChar *) "story")) {
		fprintf(stderr,"document of the wrong type, root node != story");
		xmlFreeDoc(doc);
		return;
	}

| |

Declare the pointer that will point to your parsed document.

| | |

Declare a node pointer (you'll need this in order to interact with individual nodes).

| | |

Check to see that the document was successfully parsed. If it was not, libxml will at this point register an error and stop.

| | Note | |

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

|

| | |

Retrieve the document's root element.

| | |

Check to make sure the document actually contains something.

| | |

In our case, we need to make sure the document is the right type. "story" is the root type of the documents used in this tutorial.

|


| Prev | Up | Next | | Data Types | Home | Retrieving Element Content |