/usr/share/doc/libxml2-devel/tutorial
NameSizeModeActions
images/-0755rm
apa.html21140644editdlrm
apb.html20260644editdlrm
apc.html30940644editdlrm
apd.html32320644editdlrm
ape.html30960644editdlrm
apf.html29490644editdlrm
apg.html30220644editdlrm
aph.html36000644editdlrm
api.html18990644editdlrm
ar01s02.html32980644editdlrm
ar01s03.html57180644editdlrm
ar01s04.html62230644editdlrm
ar01s05.html74220644editdlrm
ar01s06.html38490644editdlrm
ar01s07.html38660644editdlrm
ar01s08.html38940644editdlrm
ar01s09.html76030644editdlrm
includeaddattribute.c11360644editdlrm
includeaddkeyword.c13200644editdlrm
includeconvert.c18080644editdlrm
includegetattribute.c11710644editdlrm
includekeyword.c13510644editdlrm
includexpath.c14910644editdlrm
index.html59300644editdlrm
ix01.html25450644editdlrm
xmltutorial.pdf517090644editdlrm
Edit: /usr/share/doc/libxml2-devel/tutorial/ar01s07.html (3866B)
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 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);	
      

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.