• libxml2库使用示例


    解析一个名位noname.xml的文件:

    1. "1.0" standalone="no" ?>
    2. <Attributes>
    3. <Attribute name="AcquireTime" type="EPICS_PV" source="$(P)$(R)cam1:AcquireTime" dbrtype="DBR_NATIVE" description="Camera acquire time"/>
    4. <Attribute name="FilePath" type="EPICS_PV" source="$(P)$(R)$(T)FilePath" dbrtype="DBR_STRING" description="File Save Path"/>
    5. Attributes>

     解析以上文件的源文件:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include "NDAttribute.h"
    12. #include "NDAttributeList.h"
    13. #include "PVAttribute.h"
    14. #include
    15. int main()
    16. {
    17. MAC_HANDLE * macHandle;
    18. char ** macPairs;
    19. int status = 0;
    20. std::ostringstream buff;
    21. std::string buffer;
    22. std::ifstream infile;
    23. std::string fileName;
    24. std::string macrodefs;
    25. int bufferSize;
    26. fileName = "noname.xml";
    27. infile.open(fileName.c_str());
    28. buff << infile.rdbuf();
    29. buffer = buff.str();
    30. printf("The name of a xml file to parse:\n ");
    31. printf("fileName: %s\n", fileName.c_str());
    32. printf("The file's content:\n");
    33. printf("%s\n", buffer.c_str());
    34. printf("--------------------------------------------------------------\n");
    35. macCreateHandle(&macHandle, 0);
    36. macrodefs = "P=BS:,R=SIM1:,T=TIFF1:";
    37. printf("The macro defination: %s\n", macrodefs.c_str());
    38. status = macParseDefns(macHandle, macrodefs.c_str() ,&macPairs);
    39. if (status < 0){
    40. printf("macParseDefns call failed\n");
    41. return -1;
    42. }
    43. status = macInstallMacros(macHandle, macPairs);
    44. if (status < 0){
    45. printf("macInstallMacros call failed\n");
    46. return -1;
    47. }
    48. bufferSize = (int)(buffer.length() * 10);
    49. char * tmpBuffer = (char *)malloc(sizeof(char ) * bufferSize);
    50. status = macExpandString(macHandle, buffer.c_str(), tmpBuffer, bufferSize);
    51. if (status < 0){
    52. printf("macExpandString call failed\n");
    53. free(tmpBuffer);
    54. free(macPairs);
    55. return -1;
    56. }
    57. macrodefs = tmpBuffer;
    58. free(tmpBuffer);
    59. macDeleteHandle(macHandle);
    60. printf("Orignal file content: \n");
    61. printf("'%s'\n\n", buffer.c_str());
    62. printf("The expended file contenent\n");
    63. printf("'%s'\n\n", macrodefs.c_str());
    64. const char *pName, *pSource, *pAttrType, *pDescription, *pDBRType;
    65. xmlDocPtr doc;
    66. xmlNode *Attr, *Attrs;
    67. doc = xmlReadMemory(macrodefs.c_str(), (int)macrodefs.length(), NULL , NULL, 0);
    68. if (doc == NULL) {
    69. printf("xmlReadMemory call failed\n");
    70. return -1;
    71. }
    72. Attrs = xmlDocGetRootElement(doc);
    73. if ((!xmlStrEqual(Attrs->name, (const xmlChar *)"Attributes")))
    74. {
    75. printf("Cannot find the root 'Attriubtes\n'");
    76. return -1;
    77. }
    78. printf("-----------------------------------------------------------------------\n");
    79. printf("Parse the xml file: %s\n", fileName.c_str());
    80. PVAttribute * pAttribute;
    81. NDAttributeList * list = new NDAttributeList();
    82. for (Attr = xmlFirstElementChild(Attrs); Attr; Attr = xmlNextElementSibling(Attr)){
    83. pName = (const char *)xmlGetProp(Attr, (const xmlChar *)"name");
    84. if (!pName){
    85. printf("xmlGetProp '%s' call failed\n", "name");
    86. return -1;
    87. }
    88. pDescription = (const char *)xmlGetProp(Attr, (const xmlChar *)"description");
    89. if (!pDescription) pDescription ="";
    90. pSource = (const char *)xmlGetProp(Attr, (const xmlChar *)"source");
    91. if (!pSource){
    92. printf("xmlGetProp '%s' call failed\n", "source");
    93. return -1;
    94. }
    95. pAttrType = (const char *)xmlGetProp(Attr, (const xmlChar *)"type");
    96. if (!pAttrType){
    97. printf("xmlGetProp '%s' call failed\n", "type");
    98. return -1;
    99. }
    100. pDBRType = (const char *)xmlGetProp(Attr, (const xmlChar *)"dbrtype");
    101. if (!pDBRType){
    102. printf("xmlGetProp '%s' call failed\n", "dbrtype");
    103. return -1;
    104. }
    105. int dbrType = DBR_NATIVE;
    106. if (pDBRType) {
    107. if (!strcmp(pDBRType, "DBR_CHAR"))
    108. dbrType = DBR_CHAR;
    109. else if (!strcmp(pDBRType, "DBR_SHORT"))
    110. dbrType = DBR_SHORT;
    111. else if (!strcmp(pDBRType, "DBR_ENUM"))
    112. dbrType = DBR_ENUM;
    113. else if (!strcmp(pDBRType, "DBR_INT"))
    114. dbrType = DBR_INT;
    115. else if (!strcmp(pDBRType, "DBR_LONG"))
    116. dbrType = DBR_LONG;
    117. else if (!strcmp(pDBRType, "DBR_FLOAT"))
    118. dbrType = DBR_FLOAT;
    119. else if (!strcmp(pDBRType, "DBR_DOUBLE"))
    120. dbrType = DBR_DOUBLE;
    121. else if (!strcmp(pDBRType, "DBR_STRING"))
    122. dbrType = DBR_STRING;
    123. else
    124. dbrType = DBR_NATIVE;
    125. }
    126. printf("name: %s\tsource: %s\ttype: %s\tdbrtype: %s\tdescription: %s\n", pName, pSource, pAttrType, pDBRType, pDescription);
    127. pAttribute = new PVAttribute(pName, pDescription, pSource, dbrType );
    128. list->add(pAttribute);
    129. }
    130. epicsThreadSleep(1.0);
    131. printf("-----------------------------------------------------------------------\n");
    132. list->updateValues();
    133. epicsThreadSleep(1.0);
    134. printf("NDAttributeList and NDAttriubte's information: \n");
    135. list->report(stdout, 20);
    136. return 0;
    137. }

    先用通道访问测试以上此处用到的EPICS过程变量:

    1. orangepi@orangepi5:~/C_program/host_program/hostApp$ caget BS:SIM1:cam1:AcquireTime
    2. BS:SIM1:cam1:AcquireTime 5
    3. orangepi@orangepi5:~/C_program/host_program/hostApp$ caget -S BS:SIM1:TIFF1:FilePath
    4. BS:SIM1:TIFF1:FilePath /home/test_gap

    编译以上程序,执行结果如下:

    1. orangepi@orangepi5:~/C_program/host_program/hostApp$ O.linux-aarch64/testXml
    2. The name of a xml file to parse:
    3. fileName: noname.xml
    4. The file's content:
    5. --------------------------------------------------------------
    6. The macro defination: P=BS:,R=SIM1:,T=TIFF1:
    7. Orignal file content:
    8. '"1.0" standalone="no" ?>
    9. "AcquireTime" type="EPICS_PV" source="$(P)$(R)cam1:AcquireTime" dbrtype="DBR_NATIVE" description="Camera acquire time"/>
    10. "FilePath" type="EPICS_PV" source="$(P)$(R)$(T)FilePath" dbrtype="DBR_STRING" description="File Save Path"/>
    11. '
    12. The expended file contenent
    13. '"1.0" standalone="no" ?>
    14. "AcquireTime" type="EPICS_PV" source="BS:SIM1:cam1:AcquireTime" dbrtype="DBR_NATIVE" description="Camera acquire time"/>
    15. "FilePath" type="EPICS_PV" source="BS:SIM1:TIFF1:FilePath" dbrtype="DBR_STRING" description="File Save Path"/>
    16. '
    17. -----------------------------------------------------------------------
    18. Parse the xml file: noname.xml
    19. name: AcquireTime source: BS:SIM1:cam1:AcquireTime type: EPICS_PV dbrtype: DBR_NATIVE description: Camera acquire time
    20. name: FilePath source: BS:SIM1:TIFF1:FilePath type: EPICS_PV dbrtype: DBR_STRING description: File Save Path
    21. -----------------------------------------------------------------------
    22. NDAttributeList and NDAttriubte's information:
    23. NDAttributeList: address=0x556d1b5970:
    24. number of attributes=2
    25. NDAttribute, address=0x556d1b7f90:
    26. name=AcquireTime
    27. description=Camera acquire time
    28. source type=2
    29. source type string=NDAttrSourceEPICSPV
    30. source=BS:SIM1:cam1:AcquireTime
    31. dataType=NDAttrFloat64
    32. value=5.000000
    33. PVAttribute
    34. dbrType=DBR_invalid
    35. chanId=0x556d1bf3c0
    36. eventId=0x7f80000b70
    37. NDAttribute, address=0x556d1dff40:
    38. name=FilePath
    39. description=File Save Path
    40. source type=2
    41. source type string=NDAttrSourceEPICSPV
    42. source=BS:SIM1:TIFF1:FilePath
    43. dataType=NDAttrString
    44. value=/home/test_gap
    45. PVAttribute
    46. dbrType=DBR_STRING
    47. chanId=0x556d1bf3f8
    48. eventId=0x7f80000b98
  • 相关阅读:
    mysql这个数据库怎么连接
    企业做软文推广的三大错误有哪些?媒介盒子为您解答
    Python工程师Java之路(t)SpringBoot极速极简入门代码
    uniapp(uncloud) 使用生态开发接口详情4(wangeditor 富文本, 云对象, postman 网络请求)
    餐厅订位短信解决方案
    NB!移动端APP自动化安全分析平台
    python 如何处理图片 举例说明
    全面升级 | 两行代码,轻松搞定登录系统
    node+vue3+mysql前后分离开发范式——实现视频文件上传并渲染
    TwinCAT 3 首次运行报错4115
  • 原文地址:https://blog.csdn.net/yuyuyuliang00/article/details/133792369