Blame doc/tutorial/ar01s08.html
|
Packit |
423ecb |
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Retrieving Attributes</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="ar01s07.html" title="Writing Attribute"><link rel="next" href="ar01s09.html" title="Encoding Conversion"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
|
Packit |
423ecb |
Retrieving the value of an attribute is similar to the previous
|
|
Packit |
423ecb |
example in which we retrieved a node's text contents. In this case we'll
|
|
Packit |
423ecb |
extract the value of the URI we added in the previous
|
|
Packit |
423ecb |
section. Full code: Appendix G, Code for Retrieving Attribute Value Example.
|
|
Packit |
423ecb |
The initial steps for this example are similar to the previous ones: parse
|
|
Packit |
423ecb |
the doc, find the element you are interested in, then enter a function to
|
|
Packit |
423ecb |
carry out the specific task required. In this case, we call
|
|
Packit |
423ecb |
<tt class="function">getReference</tt>:
|
|
Packit |
423ecb |
|
|
Packit |
423ecb |
void
|
|
Packit |
423ecb |
getReference (xmlDocPtr doc, xmlNodePtr cur) {
|
|
Packit |
423ecb |
|
|
Packit |
423ecb |
xmlChar *uri;
|
|
Packit |
423ecb |
cur = cur->xmlChildrenNode;
|
|
Packit |
423ecb |
while (cur != NULL) {
|
|
Packit |
423ecb |
if ((!xmlStrcmp(cur->name, (const xmlChar *)"reference"))) {
|
|
Packit |
423ecb |
uri = xmlGetProp(cur, "uri");
|
|
Packit |
423ecb |
printf("uri: %s\n", uri);
|
|
Packit |
423ecb |
xmlFree(uri);
|
|
Packit |
423ecb |
}
|
|
Packit |
423ecb |
cur = cur->next;
|
|
Packit |
423ecb |
}
|
|
Packit |
423ecb |
return;
|
|
Packit |
423ecb |
}
|
|
Packit |
423ecb |
|
|
Packit |
423ecb |
|
|
Packit |
423ecb |
|
|
Packit |
423ecb |
The key function is <tt class="function">xmlGetProp</tt>, which returns an
|
|
Packit |
423ecb |
<tt class="varname">xmlChar</tt> containing the attribute's value. In this case,
|
|
Packit |
423ecb |
we just print it out.
|
|
Packit |
423ecb |
| Note |
---|
|
|
|
Packit |
423ecb |
If you are using a DTD that declares a fixed or
|
|
Packit |
423ecb |
default value for the attribute, this function will retrieve it.
|
|
Packit |
423ecb |
|
|
Packit |
423ecb |
|
|
Packit |
423ecb |
|
|
Packit |
423ecb |
</body></html>
|