3

拿起第一个仪表节点后,我找不到下一个仪表节点。似乎 mxmlGetNextSibling(...) 应该这样做,但我得到一个空值。我尝试了 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);    
    
}

4

0 回答 0