zoukankan      html  css  js  c++  java
  • libxml的使用(2)--读取节点属性

     

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

    [html] view plaincopyprint?
     
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <root>  
    3. <node1>content1</node1>  
    4. <node2 attribute="yes">content2</node2>  
    5. <node3>  
    6. <subnode>go</subnode>  
    7. </node3>  
    8. </root>  

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

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

    [cpp] view plaincopyprint?
     
     
    1. xmlChar* attr_value = NULL;  
    2. if(!xmlStrcmp(node->name, (const xmlChar*)"node2")) {  
    3.     attr_value = xmlGetProp(node, "attribute");  
    4.     printf("attribute value:%s ",attr_value);  
    5.     xmlFree(attr_value);  
    6. }  
    这样我们就提取出了yes这个字符串了!
     
     
  • 相关阅读:
    java 运算
    java String
    java的数据类型
    Python: str() 和 repr() 的区别
    Linux命令:which
    Linux命令:locate
    Linux命令:ifconfig
    Linux命令:whereis
    Linux命令:rz 和 sz
    Linux命令:scp
  • 原文地址:https://www.cnblogs.com/fire909090/p/6798152.html
Copyright © 2011-2022 走看看