TinyXML2 advance to next sibling

2.2k Views Asked by At

I'm not sure how to format the XML file, so here is a direct link if it is unreadable in the stackoverflow code format.

XML Description:

test = root
name = child of test
numofIndx -> dwellms = subchildren of name
index = child of test

- <test>
- <name index="2-6">
  <numOfIndx>5</numOfIndx> 
  <freqStart>1.0</freqStart> 
  <freqStop>49.405</freqStop> 
  <numOfPoints>75</numOfPoints> 
  <attenuation>10</attenuation> 
  <dwellms>10</dwellms> 
  </name>
- <index number="2">
  <freqStart>1.0</freqStart> 
  <freqStep>0.1</freqStep> 
  <level>-16</level> 
  <numOfPoints>8</numOfPoints> 
  </index>
+ <index number="3">
  <freqStart>1.8</freqStart> 
  <freqStep>0.1</freqStep> 
  <level>-9</level> 
  <numOfPoints>2</numOfPoints> 
  </index>
+ <index number="4">
  <freqStart>2.0</freqStart> 
  <freqStep>0.1</freqStep> 
  <level>-1</level> 
  <numOfPoints>1</numOfPoints> 
  </index>
+ <index number="5">
  <freqStart>2.1</freqStart> 
  <freqStep>0.1</freqStep> 
  <level>-1</level> 
  <numOfPoints>4</numOfPoints> 
  </index>
+ <index number="6">
  <freqStart>2.5</freqStart> 
  <freqStep>0.795</freqStep> 
  <level>-1</level> 
  <numOfPoints>60</numOfPoints> 
  </index>
- <name index="14-15">
  <numOfIndx>2</numOfIndx> 
  <freqStart>705.45203</freqStart> 
  <freqStop>900</freqStop> 
  <numOfPoints>392</numOfPoints> 
  <attenuation>55</attenuation> 
  <dwellms>10</dwellms> 
  </name>
+ <index number="14">
  <freqStart>705</freqStart> 
  <freqStep>0.5</freqStep> 
  <level>-60</level> 
  <numOfPoints>41</numOfPoints> 
  </index>
+ <index number="15">
  <freqStart>725</freqStart> 
  <freqStep>0.5</freqStep> 
  <level>-50</level> 
  <numOfPoints>351</numOfPoints> 
  </index>
  </test>

I am trying to read the XML data into a structure. Currently, I have no problem reading all of the data from index="2-6". However, I can't seem to navigate to index="14-15" on my second loop. I've tried to debug, but I can't seem to figure out why my "advance to next sibling code" works correctly for "index" siblings, but not for "name" siblings.

When I run my code, the 2nd loop (i = 1) fails when getAttr attempts to return pElement which is NULL.
Error message:

http://i.imgur.com/s5yT6nC.png

Relevant code: main:

 //Fill test structure loop
    for (int i{ 0 }; i < 2; i++)
        {
            test[i].testName = xml.getAttr("name", "index", i);
            test[i].noi = xml.getInt("name", "numOfIndx", i);
            test[i].freqStart = xml.getFloat("name", "freqStart", i);
            test[i].freqStop = xml.getFloat("name", "freqStop", i);
            test[i].points = xml.getInt("name", "numOfPoints", i);
            test[i].att = xml.getInt("name", "attenuation", i);
            test[i].dwel = xml.getInt("name", "dwellms", i);

            for (int j{ 0 }; j < test[i].noi; j++)
            {
                temp = xml.getAttr("index", "number", j);
                test[i].idx[j].index = atoi(temp);
                test[i].idx[j].freqStart = xml.getFloat("index", "freqStart", j);
                test[i].idx[j].freqStep = xml.getFloat("index", "freqStep", j);
                test[i].idx[j].level = xml.getInt("index", "level", j);
                test[i].idx[j].points = xml.getInt("index", "numOfPoints", j);
            }
        }

xml.getAttr/float/int are all essentially the same, so I will just post getAttr
getAttr:

const char * myXML::getAttr(const char * child, const char * attr, int sibNum)
{
    //Locate root
    XMLNode *pRoot{ findRoot() };
    if (pRoot == nullptr) { return "Error"; }

    XMLElement* pElement{ pRoot->FirstChildElement(child) };
    //If child has identical sibling
    for (int i{ 0 }; i < sibNum; i++)
    {
        //Find that sibling number
        //pElement = nextRel(pElement);
        pElement = pElement->NextSiblingElement();
    }
    if (pElement == nullptr || NULL) { return "Error"; }

    return pElement->Attribute(attr);
}


findRoot():

XMLNode * myXML::findRoot()
{
    XMLNode * pRoot{ doc.FirstChild() };
    if (pRoot == nullptr) { return nullptr; }
    return pRoot;
}
1

There are 1 best solutions below

0
On

Solved. It used the same root for i=0 and i=1 but the next sibling element would change when i=1 because the next sibling is "index". I altered the code as below:

const char * myXML::getAttr(const char * child, const char * attr, int sibNum)
    {
        //Locate root
        XMLNode *pRoot{ findRoot() };
        if (pRoot == nullptr) { return "Error"; }

        XMLElement* pElement{ pRoot->FirstChildElement(child) };
        //If child has identical sibling
    for (int i{ 0 }; i < sibNum; i++)
    {
        //Find that sibling number
        //pElement = nextRel(pElement);
        pElement = pElement->NextSiblingElement(child);
    }
    if (pElement == nullptr || NULL) { return "Error"; }

    return pElement->Attribute(attr);
}

This seemed to fix things as it looks for the next sibling of the parameter name child instead of just automatically taking the adjacent sibling.