Source Code File 06-04_attributes
06-04_attributes.c
#include <stdio.h>
#include <stdlib.h>
#include <libxml/parser.h>
void get_attrib(xmlNodePtr n,const xmlChar *a)
{
xmlNodePtr cur;
xmlChar *attrib;
for(cur=n; cur; cur=cur->next)
{
if( cur->type==XML_ELEMENT_NODE )
{
attrib = xmlGetProp(cur,a);
if( attrib!=NULL )
printf("<%s> has '%s' attribute: %s\n",
cur->name,
a,
attrib
);
xmlFree(attrib);
if( cur->children )
get_attrib(cur->children,a);
}
}
}
int main()
{
const char filename[] = "sample.xml";
xmlDocPtr doc;
xmlNodePtr root;
doc = xmlParseFile(filename);
if( doc==NULL )
{
fprintf(stderr,"Unable to open %s\n",filename);
exit(1);
}
root = xmlDocGetRootElement(doc);
get_attrib(root,(const xmlChar *)"type");
xmlFreeDoc(doc);
return(0);
}
Output
<phone> has 'type' attribute: lab
<phone> has 'type' attribute: mobile
Copyright © 1997-2025 by QPBC.
All rights reserved
