/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/ar01s08.html (3894B)
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 G, Code for Retrieving Attribute Value Example.

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) {

	xmlChar *uri;
	cur = cur->xmlChildrenNode;
	while (cur != NULL) {
	    if ((!xmlStrcmp(cur->name, (const xmlChar *)"reference"))) {
		   1 uri = xmlGetProp(cur, "uri");
		    printf("uri: %s\n", uri);
		    xmlFree(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]Note

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