libxml2库使用示例

2023-10-20 08:44
文章标签 使用 示例 libxml2

本文主要是介绍libxml2库使用示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

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

 解析以上文件的源文件:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sstream>
#include <fstream>
#include <epicsThread.h>
#include <macLib.h>
#include "NDAttribute.h"
#include "NDAttributeList.h"
#include "PVAttribute.h"
#include <libxml/parser.h>int main()
{MAC_HANDLE * macHandle;char ** macPairs;int status = 0;std::ostringstream buff;std::string buffer;std::ifstream infile;std::string fileName;std::string macrodefs;int bufferSize;fileName = "noname.xml";infile.open(fileName.c_str());buff << infile.rdbuf();buffer = buff.str();printf("The name of a xml file to parse:\n ");printf("fileName: %s\n", fileName.c_str());printf("The file's content:\n");printf("%s\n", buffer.c_str());printf("--------------------------------------------------------------\n");macCreateHandle(&macHandle, 0);macrodefs = "P=BS:,R=SIM1:,T=TIFF1:";printf("The macro defination:  %s\n", macrodefs.c_str());status = macParseDefns(macHandle, macrodefs.c_str() ,&macPairs);if (status < 0){printf("macParseDefns call failed\n");return -1;}status = macInstallMacros(macHandle, macPairs);if (status < 0){printf("macInstallMacros call failed\n");return -1;}bufferSize = (int)(buffer.length() * 10);char * tmpBuffer = (char *)malloc(sizeof(char ) * bufferSize);status = macExpandString(macHandle, buffer.c_str(), tmpBuffer, bufferSize);if (status < 0){printf("macExpandString call failed\n");free(tmpBuffer);free(macPairs);return -1;}macrodefs = tmpBuffer;free(tmpBuffer);macDeleteHandle(macHandle);printf("Orignal file content: \n");printf("'%s'\n\n", buffer.c_str());printf("The expended file contenent\n");printf("'%s'\n\n", macrodefs.c_str());const char *pName, *pSource, *pAttrType, *pDescription, *pDBRType;xmlDocPtr doc;xmlNode *Attr, *Attrs;doc = xmlReadMemory(macrodefs.c_str(), (int)macrodefs.length(), NULL , NULL, 0);if (doc == NULL) {printf("xmlReadMemory call failed\n");return -1;}Attrs = xmlDocGetRootElement(doc);if ((!xmlStrEqual(Attrs->name, (const xmlChar *)"Attributes"))){printf("Cannot find the root 'Attriubtes\n'");return -1;}printf("-----------------------------------------------------------------------\n");printf("Parse the xml file: %s\n", fileName.c_str());PVAttribute * pAttribute;NDAttributeList * list = new NDAttributeList();for (Attr = xmlFirstElementChild(Attrs); Attr; Attr = xmlNextElementSibling(Attr)){pName = (const char *)xmlGetProp(Attr, (const xmlChar *)"name");if (!pName){printf("xmlGetProp '%s' call failed\n", "name");return -1;}pDescription = (const char *)xmlGetProp(Attr, (const xmlChar *)"description");if (!pDescription) pDescription ="";pSource = (const char *)xmlGetProp(Attr, (const xmlChar *)"source");if (!pSource){printf("xmlGetProp '%s' call failed\n", "source");return -1;}pAttrType = (const char *)xmlGetProp(Attr, (const xmlChar *)"type");if (!pAttrType){printf("xmlGetProp '%s' call failed\n", "type");return -1;}pDBRType = (const char *)xmlGetProp(Attr, (const xmlChar *)"dbrtype");if (!pDBRType){printf("xmlGetProp '%s' call failed\n", "dbrtype");return -1;}int dbrType = DBR_NATIVE;if (pDBRType) {if      (!strcmp(pDBRType, "DBR_CHAR"))dbrType = DBR_CHAR;else if (!strcmp(pDBRType, "DBR_SHORT"))dbrType = DBR_SHORT;else if (!strcmp(pDBRType, "DBR_ENUM"))dbrType = DBR_ENUM;else if (!strcmp(pDBRType, "DBR_INT"))dbrType = DBR_INT;else if (!strcmp(pDBRType, "DBR_LONG"))dbrType = DBR_LONG;else if (!strcmp(pDBRType, "DBR_FLOAT"))dbrType = DBR_FLOAT;else if (!strcmp(pDBRType, "DBR_DOUBLE"))dbrType = DBR_DOUBLE;else if (!strcmp(pDBRType, "DBR_STRING"))dbrType = DBR_STRING;elsedbrType = DBR_NATIVE;}printf("name: %s\tsource: %s\ttype: %s\tdbrtype: %s\tdescription: %s\n", pName, pSource, pAttrType, pDBRType, pDescription);pAttribute = new PVAttribute(pName, pDescription, pSource, dbrType );list->add(pAttribute);}epicsThreadSleep(1.0);printf("-----------------------------------------------------------------------\n");list->updateValues();epicsThreadSleep(1.0);printf("NDAttributeList and NDAttriubte's information: \n");list->report(stdout, 20);return 0;
}

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

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

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

orangepi@orangepi5:~/C_program/host_program/hostApp$ O.linux-aarch64/testXml
The name of a xml file to parse:fileName: noname.xml
The file's content:
<?xml version="1.0" standalone="no" ?>
<Attributes><Attribute name="AcquireTime" type="EPICS_PV" source="$(P)$(R)cam1:AcquireTime" dbrtype="DBR_NATIVE"  description="Camera acquire time"/><Attribute name="FilePath" type="EPICS_PV" source="$(P)$(R)$(T)FilePath"  dbrtype="DBR_STRING" description="File Save Path"/>
</Attributes>--------------------------------------------------------------
The macro defination:  P=BS:,R=SIM1:,T=TIFF1:
Orignal file content:
'<?xml version="1.0" standalone="no" ?>
<Attributes><Attribute name="AcquireTime" type="EPICS_PV" source="$(P)$(R)cam1:AcquireTime" dbrtype="DBR_NATIVE"  description="Camera acquire time"/><Attribute name="FilePath" type="EPICS_PV" source="$(P)$(R)$(T)FilePath"  dbrtype="DBR_STRING" description="File Save Path"/>
</Attributes>
'The expended file contenent
'<?xml version="1.0" standalone="no" ?>
<Attributes><Attribute name="AcquireTime" type="EPICS_PV" source="BS:SIM1:cam1:AcquireTime" dbrtype="DBR_NATIVE"  description="Camera acquire time"/><Attribute name="FilePath" type="EPICS_PV" source="BS:SIM1:TIFF1:FilePath"  dbrtype="DBR_STRING" description="File Save Path"/>
</Attributes>
'-----------------------------------------------------------------------
Parse the xml file: noname.xml
name: AcquireTime       source: BS:SIM1:cam1:AcquireTime    type: EPICS_PV  dbrtype: DBR_NATIVE     description: Camera acquire time
name: FilePath  source: BS:SIM1:TIFF1:FilePath      type: EPICS_PV  dbrtype: DBR_STRING     description: File Save Path
-----------------------------------------------------------------------
NDAttributeList and NDAttriubte's information:NDAttributeList: address=0x556d1b5970:number of attributes=2NDAttribute, address=0x556d1b7f90:name=AcquireTimedescription=Camera acquire timesource type=2source type string=NDAttrSourceEPICSPVsource=BS:SIM1:cam1:AcquireTimedataType=NDAttrFloat64value=5.000000PVAttributedbrType=DBR_invalidchanId=0x556d1bf3c0eventId=0x7f80000b70NDAttribute, address=0x556d1dff40:name=FilePathdescription=File Save Pathsource type=2source type string=NDAttrSourceEPICSPVsource=BS:SIM1:TIFF1:FilePathdataType=NDAttrStringvalue=/home/test_gapPVAttributedbrType=DBR_STRINGchanId=0x556d1bf3f8eventId=0x7f80000b98

这篇关于libxml2库使用示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/246012

相关文章

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示