A
A
AlexSer2019-11-12 10:53:36
Qt
AlexSer, 2019-11-12 10:53:36

How to parse Xml data received via serial in QT?

Through the Emulator I send this line to the com port

<FileIndex>
    <Name>lol.xml</Name>
    <Size>0</Size>
    <Path>E:/test/build-untitled-Desktop_Qt_5_4_1_MinGW_32bit-Release/lol.xml</Path>
    <Created>Сб мар 14 21:55:37 2015</Created>
    <Name>Makefile</Name>
    <Size>20399</Size>
    <Path>E:/test/build-untitled-Desktop_Qt_5_4_1_MinGW_32bit-Release/Makefile</Path>
    <Created>Чт мар 12 19:00:45 2015</Created>
</FileIndex>

Here is the receiving and parsing code:
xmlReader=new QXmlStreamReader();

    QByteArray byte;
    byte=serialPort->readAll();
    QString str=QString(byte);

    //добавляем в xml

    xmlReader->addData(str);
    while(!xmlReader->atEnd())
        {
            QXmlStreamReader::TokenType token = xmlReader->readNext();
            if(token == QXmlStreamReader::StartElement) {
                if(xmlReader->name() == "name") {
                    xmlReader->readNext();
                   qDebug()<<xmlReader->text().toString();
                }
            }
        }

but there is no output, what should I pay attention to?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel K, 2019-11-12
@PavelK

Data through QSerialPort comes in chunks, not all at once, so you need to collect them all first, and only then parse.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question