拿起第一个测距节点后,我找不到下一个测距节点。看来mxmlGetNextSibling(...)应该这样做,但是我得到了null。我尝试了min-XML函数的各种组合,但无法找出我做错了什么。
我已附加一个相关的事件代码段,其中显示了我正在使用的XML和基本代码(为清楚起见,删除了一些仪表节点的子代)
注意:使用此for循环进行迭代
for(pGaugeNode = mxmlFindElement(tree,tree,“ gauge”,NULL,NULL,MXML_DESCEND); pGaugeNode!= NULL; pGaugeNode = mxmlFindElement(pGaugeNode,tree,“ gauge”,NULL,NULL,MXML_DESCEND))>
//XML file
<?xml version="1.0" encoding="UTF-8"?>
<DialsConfiguration>
<ucbPort>50000</ucbPort>
<!-- copy gauge as many times as the number of displays you want, and modify parameters accordingly -->
<gauge>
<name>Rabbit</name>
<unitsName>degC</unitsName>
</gauge>
<gauge>
<name>MyTest</name>
<unitsName>Temp°C</unitsName>
</gauge>
<!-- etc -->
</DialsConfiguration>
#define _GNU_SOURCE
#include <mxml.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
struct Display
{
char nameStr[32];
char unitsStr[32];
};
struct Display Displays[ 64 ];
int gUCBPort = 50000;
void initDisplayArrays( void );
int main(int argc, char *argv[])
{
initDisplayArrays(); //Initialize the display arrays to XML config file values
}
void initDisplayArrays( void )
{
int i=0;
char* pStr;
char* pNodeStr = NULL;
mxml_node_t* pNode = NULL;
mxml_node_t* pGaugeNode = NULL;
mxml_node_t* pTopNode = NULL;
mxml_node_t* pMyNode = NULL;
mxml_node_t* pThisNode = NULL;
//Now load the XML file
FILE *fp;
mxml_node_t *tree;
fp = fopen("dialsTest.xml", "r");
tree = mxmlLoadFile(NULL, fp, MXML_OPAQUE_CALLBACK); //Load the XML file
pNode = mxmlFindElement(tree, tree, "ucbPort", NULL, NULL, MXML_DESCEND);
if (pNode != NULL) {gUCBPort = atoi( mxmlGetOpaque( pNode ) );} //Get the UCB port number
//*************************************************************************
pGaugeNode = mxmlFindElement(tree, tree, "gauge", NULL, NULL, MXML_DESCEND); //Find the first gauge node
pTopNode = mxmlFindElement(tree, tree, "DialsConfiguration", NULL, NULL, MXML_DESCEND); //Find the top node
i=0;
while (pGaugeNode != NULL ) //Cycle through the gauges until no more are to be displayed
{
pThisNode = mxmlGetFirstChild( pGaugeNode ); //This points to the first node of the gauge WRONG?
pNode = mxmlFindElement(pThisNode, pTopNode, "name", NULL, NULL, MXML_NO_DESCEND);
pStr = (char*)mxmlGetOpaque( pNode );
strcpy( Displays[i].nameStr, pStr );
pNode = mxmlFindElement(pThisNode, pTopNode, "unitsName", NULL, NULL, MXML_NO_DESCEND);
pStr = (char*)mxmlGetOpaque( pNode );
strcpy( Displays[i].unitsStr, pStr );
//Increment for next gauge Display
if (i < 10) i++; else break;
pGaugeNode = mxmlGetNextSibling( pGaugeNode );
}
if (tree) mxmlDelete(tree); //Clean up after loading parameters
if (fp) fclose(fp);
}