libxml2库使用示例

编程入门 行业动态 更新时间:2024-10-10 09:17:29

libxml2库使用<a href=https://www.elefans.com/category/jswz/34/1770116.html style=示例"/>

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库使用示例

本文发布于:2023-12-07 06:42:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1670433.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!