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;
                        else
                                 dbrType = 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=2

NDAttribute, address=0x556d1b7f90:
  name=AcquireTime
  description=Camera acquire time
  source type=2
  source type string=NDAttrSourceEPICSPV
  source=BS:SIM1:cam1:AcquireTime
  dataType=NDAttrFloat64
  value=5.000000
  PVAttribute
    dbrType=DBR_invalid
    chanId=0x556d1bf3c0
    eventId=0x7f80000b70

NDAttribute, address=0x556d1dff40:
  name=FilePath
  description=File Save Path
  source type=2
  source type string=NDAttrSourceEPICSPV
  source=BS:SIM1:TIFF1:FilePath
  dataType=NDAttrString
  value=/home/test_gap
  PVAttribute
    dbrType=DBR_STRING
    chanId=0x556d1bf3f8
    eventId=0x7f80000b98
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值