3

I cannot find the next gauge node after picking up the first. It seems that mxmlGetNextSibling(...) ought to do it, but I get a null. I have tried various combinations of min-XML functions but cannot find out what I am doing wrong.

I have attached a relevent code snippet showing the XML and basic code I am using (with some children of the gauge nodes removed for clarity)

Note: Iterating using this for loop does work

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);    
    
}
Dirk Bruere
  • 237
  • 2
  • 15
  • Given the paucity of examples and so little expertise available even here, I would not reccommend anyone use mini-xml. I wish I had not done so. – Dirk Bruere Jan 16 '19 at 09:13

0 Answers0