Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Pass QXmlStreamReader Instance to Class



  • I am trying to read an XML document using QXmlStreamReader. I am running into issues when I try to pass a pointer to the reader to other classes. I create an instance of the reader in one class. That class reads the XML until it comes to a block defining a new class. I create and instance of the new class and then call a function in that class to continue reading the XML specific for it. For instance:

    @void SF_UnitClass::ReadModes()
    {
    Q_ASSERT(XML.isStartElement() && XML.name() == MODES);
    NumModes = XML.attributes().value(COUNT).toInt();
    while (XML.readNextStartElement())
    {
    if (XML.name() == MODE)
    {
    ModeClass* pMode = new ModeClass(this);
    ModeList += XML.attributes().value(ID).toString();
    pMode->ReadXML(&XML);
    {
    }
    }
    else
    {
    XML.raiseError(QObject::tr("Something other than Mode block encountered in Modes block"));
    }
    }
    }
    @
    This works part way through the file but then the reader seems to forget what level its at. readNextStartElement() returns false when it shouldn't. The file is fine as I took a TreeWidget example and customized it for my use and it reads the whole file just fine. I am thinking that for some reason I cannot pass a pointer to the reader to another classes function to continue reading the XML?


  • Qt Champions 2017

    You can pass. We use everywhere. You can pass. Some of the sample methods we have are

    @ void readCompanyNew(QXmlStreamReader *par);
    void readProjectNew(QXmlStreamReader *par);

    I am calling them like
    readCompanyNew(&parser);
    readProjectNew(&parser);

    @



  • Thanks, that is good to know. I will keep digging into this then.


  • Qt Champions 2017

    if you want the complete sample, drop email to me. I will send you the complete solution dump.



  • Thanks but I think its in my code somewhere but I am missing it. I am finding it hard to debug because I don't know why readNextStartElement() is returning a false where it should not. Here is a sample of my XML. My code reads all the <Channel> blocks fine. It then it reads the <Servos> start element and the first <Servo> block fine but then gets a false from readNextStartElement() when it should read the next <Servo> start element and goes all the way back up to my Mode loop. Just need to figure out why it does not read the second servo block. Thanks.

    @<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xml>
    <SmartFlyUnit version="1.0">
    <UnitName>PowerExpander Eq10E</UnitName>
    <UnitCode>PE-5</UnitCode>
    <UnitID>1</UnitID>
    <MinRev>1.0</MinRev>
    <MaxRev>1.9</MaxRev>
    <Servos>36</Servos>
    <Outputs>32</Outputs>
    <UnitMode>Chan</UnitMode>
    <Modes Count="2">
    <Mode ID="Chan">
    <Receivers>1</Receivers>
    <Channels Count="10">
    <Channel ID="0">
    <UserName></UserName>
    <EndPtHold>Off</EndPtHold>
    <ServoPri>Off</ServoPri>
    </Channel>
    <Channel ID="1">
    <UserName></UserName>
    <EndPtHold>Off</EndPtHold>
    <ServoPri>Off</ServoPri>
    </Channel>
    </Channels>
    <Servos Count="36">
    <Servo ID="0">
    <Offset>0</Offset>
    <PosFact>1.0000</PosFact>
    <NegFact>1.0000</NegFact>
    <Reverse>Off</Reverse>
    <SecServo>0xFF</SecServo>
    <Unit>0xFF</Unit>
    <Ser1>0xFF</Ser1>
    <Ser2>0xFF</Ser2>
    <Ser3>0xFF</Ser3>
    </Servo>
    <Servo ID="1">
    <Offset>0</Offset>
    <PosFact>1.0000</PosFact>
    <NegFact>1.0000</NegFact>
    <Reverse>Off</Reverse>
    <SecServo>0xFF</SecServo>
    <Unit>0xFF</Unit>
    <Ser1>0xFF</Ser1>
    <Ser2>0xFF</Ser2>
    <Ser3>0xFF</Ser3>
    </Servo>
    </Servos>
    </Mode>
    </Modes>
    <MCUs Count="1">
    <MCU ID="0">
    <FileName>/PE-5/PE-5_M0_1_00.sffw</FileName>
    </MCU>
    </MCUs>
    <FPGAs Count="1">
    <FPGA ID="0">
    <Configs Count="2">
    <Config ID="0">
    <FileName>/PE-5/PE-5_F0C0_1_00.sffw</FileName>
    </Config>
    <Config ID="1">
    <FileName>/PE-5/PE-5_F0C1_1_00.sffw</FileName>
    </Config>
    </Configs>
    </FPGA>
    </FPGAs>
    </SmartFlyUnit>
    @



  • OK, solved. My inexperience in dealing with the XML reader. I missed a case in my switch statement which caused an item to be missed and put things out of sequence. I need to add a default with a skipCurrentElement or raise an error. Thanks for all the feedback, it helped me catch this.


Log in to reply