libxml的使用2--读取节点属性

http://blog.csdn.net/hdutigerkin/article/details/7546907

上一篇文章当中,我读取了各个节点的名字和内容,现在我将读取各个节点的属性

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <root>
  3. <node1>content1</node1>
  4. <node2attribute="yes">content2</node2>
  5. <node3>
  6. <subnode>go</subnode>
  7. </node3>
  8. </root>

这是上一篇文章中提到的xml文件

在node2这个节点上有一个属性attribute,其值是yes。我们可以使用xmlGetProp这个函数将其提取出来。这个函数包含了两个变量,一个是将要提取属性的节点指针xmlNodePtr,另一个属性名称

[cpp] ?
    xmlChar*attr_value=NULL;
  1. if(!xmlStrcmp(node->name,(constxmlChar*)"node2")){
  2. attr_value=xmlGetProp(node,"attribute");
  3. printf("attributevalue:%s\n",attr_value);
  4. xmlFree(attr_value);
  5. }
这样我们就提取出了yes这个字符串了!

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念