"Fossies" - the Fresh Open Source Software archive

Member "openvalidator-2.7.1/libxml2-2.6.28/doc/tutorial/ar01s07.html" (31 Oct 2010, 3866 Bytes) of archive /linux/www/openvalidator_2.7.1.tar.gz:

Caution: In this restricted "Fossies" environment the current HTML page may not be correctly presentated and may have some non-functional links. Alternatively you can here view or download the uninterpreted source code. That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.

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 F, Code for Add Attribute Example.

A reference is a child of the story element, so finding the place to put our new element and attribute is simple. As soon as we do the error-checking test in our parseDoc, we are in the right spot to add our element. But before we do that, we need to make a declaration using a data type we have not seen yet:

	xmlAttrPtr newattr;

We also need an extra xmlNodePtr:

	xmlNodePtr newnode;

The rest of parseDoc is the same as before until we check to see if our root element is story. If it is, then we know we are at the right spot to add our element:

	1 newnode = xmlNewTextChild (cur, NULL, "reference", NULL);
	2 newattr = xmlNewProp (newnode, "uri", uri);	


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.