我正在尝试使用QXmlStreamReader读取XML文档。当我尝试将指针传递给其他类时,我遇到了问题。我在一个类中创建了一个读者实例。该类读取XML直到定义新类的块。我创建新类的实例,然后调用该类中的函数继续读取特定于它的XML。例如:
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"));
}
}
}
void ModeClass::ReadXML(QXmlStreamReader* pXML)
{
Q_ASSERT(pXML->isStartElement() && pXML->name() == MODE);
while (pXML->readNextStartElement())
{
int nameIndex = ModeList.indexOf(pXML->name().toString());
if(nameIndex != -1)
{
switch(nameIndex)
{
case 0:
{
qDebug() << "Mode: Receivers";
NumReceivers = pXML->readElementText().toInt();
break;
}
case 1:
{
qDebug() << "Mode: Channels";
ReadChannels(pXML);
break;
}
case 2:
{
qDebug() << "Mode: Servos";
ReadServos(pXML);
break;
}
}
}
else
{
qDebug() << "Mode: Error " << name;
pXML->raiseError(QObject::tr("Unrecognized keyword for Mode"));
}
}
}
void ModeClass::ReadChannels(QXmlStreamReader* pXML)
{
Q_ASSERT(pXML->isStartElement() && pXML->name() == CHANNELS);
NumChannels = pXML->attributes().value(COUNT).toInt();
while (pXML->readNextStartElement())
{
if (pXML->name() == CHANNEL)
{
qDebug() << "Mode: Read Channel";
ChannelClass* pChannel = new ChannelClass(this);
pChannel->ReadXML(pXML);
}
else
{
pXML->raiseError(QObject::tr("Something other than Channel block enc
}
}
}
void ModeClass::ReadServos(QXmlStreamReader* pXML)
{
Q_ASSERT(pXML->isStartElement() && pXML->name() == SERVOS);
NumServos = pXML->attributes().value(COUNT).toInt();
while (pXML->readNextStartElement())
{
if (pXML->name() == SERVO)
{
qDebug() << "Mode: Read Servo";
ServoClass* pServo = new ServoClass(this);
pServo->ReadXML(pXML);
}
else
{
qDebug() << "Servos: raiseError";
pXML->raiseError(QObject::tr("Something other than Servo block encou
}
}
qDebug() << "Mode: Read Servos Exit";
}
void ChannelClass::ReadXML(QXmlStreamReader* pXML)
{
Q_ASSERT(pXML->isStartElement() && pXML->name() == CHANNEL);
ChannelNumber = pXML->attributes().value(ID).toInt();
while (pXML->readNextStartElement())
{
int nameIndex = ChannelList.indexOf(pXML->name().toString());
if(nameIndex != -1)
{
switch(nameIndex)
{
case 0:
{
UserName = pXML->readElementText();
break;
}
case 1:
{
EndPointHold = String2Bool(pXML->readElementText());
break;
}
case 2:
{
ServoPriority = String2Bool(pXML->readElementText());
break;
}
}
}
else
{
pXML->raiseError(QObject::tr("Unrecognized keyword for Channel"));
}
}
}
void ServoClass::ReadXML(QXmlStreamReader* pXML)
{
Q_ASSERT(pXML->isStartElement() && pXML->name() == SERVO);
ServoNumber = pXML->attributes().value(ID).toInt();
while (pXML->readNextStartElement())
{
int nameIndex = ServoList.indexOf(pXML->name().toString());
qDebug() << "Servo: NameIndex: " << nameIndex;
if(nameIndex != -1)
{
switch(nameIndex)
{
case 0:
{
Offset = pXML->readElementText().toInt();
break;
}
case 1:
{
PosFactor = pXML->readElementText().toInt();
break;
}
case 2:
{
NegFactor = pXML->readElementText().toInt();
break;
}
case 3:
{
SecServo = pXML->readElementText().toInt();
break;
}
case 4:
{
Unit = pXML->readElementText().toInt();
break;
}
case 5:
{
Ser1 = pXML->readElementText().toInt();
break;
}
case 6:
{
Ser2 = pXML->readElementText().toInt();
break;
}
case 7:
{
Ser3 = pXML->readElementText().toInt();
break;
}
}
}
else
{
qDebug() << "Servo: raiseError";
pXML->raiseError(QObject::tr("Unrecognized keyword for Servo block")
}
}
qDebug() << "Servo: Exit ";
}
<?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>
我添加了ModeClass用于识别通道和伺服块的代码。我还添加了ChannelClass和ServoClass的代码。最后,我添加了我的XML文件的编辑版本,但足以显示问题。这个XML可以很好地读入我的TreeWidget代码(未显示)。当我在上面的代码中读到它时会发生的是所有通道都被正确读取,然后它读取第一个伺服块并一直返回到主ModeClass循环,而不是读取ModeClass :: ReadServos中的下一个伺服。我知道这是很多代码,但它似乎是显示问题的唯一方法。谢谢,
答案 0 :(得分:0)
Qt docs说:
读取当前元素中的下一个start元素。返回 到达start元素时为true。当结束元素是 到达,或发生错误时,返回false。
因此,当您遇到start元素时,使用readNextStartElement()
将在您找到的元素内搜索
即
<xml>
</xml>
<xml>
</xml>
这里会发生什么:
readNextStartElement()
将停在第一个<xml>
节点readNextStartElement()
将返回false,因为它会找到</xml>
在一点点修改过的例子中:
<xml>
<child>
</child>
</xml>
<xml>
</xml>
readNextStartElement()
将停在第一个<xml>
节点readNextStartElement()
将停在<child>
节点readNextStartElement()
将返回false,因为<child>
node 到达节点后,您感兴趣,并阅读所需的所有信息,您必须致电skipCurrentElement()
,以便QXmlStreamReader
将转到当前元素的末尾
所以先做一点样品:
<xml>
</xml>
<xml>
</xml>
readNextStartElement()
将停在第一个<xml>
节点skipCurrentElement()
将在关闭第一个<xml>
节点readNextStartElement()
将停在第二个<xml>
节点