Home > OS >  How does the characterOffset method of QXmlStreamReader work?
How does the characterOffset method of QXmlStreamReader work?

Time:02-24

The characterOffset method stops working properly if the xml file is longer than a few lines. With an increase in the amount of information in the xml file, the characterOffset method deviates from the position of the tag even more.

Code:

QFile file("../file-multi-hello.xml");
file.open(QIODevice::ReadOnly);

QXmlStreamReader xml;
xml.setDevice(&file);
qint64 startPositionElem = -1;
qint64 endPositionElem = -1;
while(!xml.atEnd()) {
    qint64 offset = xml.characterOffset();
    xml.readNext();
    if (xml.isStartElement() && xml.name().toString() == "t") {
        startPositionElem = offset;
    }
    if (xml.isEndElement() && xml.name().toString() == "t") {
        endPositionElem = xml.characterOffset();
    }
}

file.seek(startPositionElem);
qDebug() << file.read(endPositionElem - startPositionElem);

file-multi-hello.xml

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14">
    <w:body>
        <w:p w14:paraId="4807E431" w14:textId="62BDDD69" w:rsidR="00E14DC7" w:rsidRDefault="00E14DC7">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>Hello world</w:t>
            </w:r>
        </w:p>
        <w:p w14:paraId="5029BEAE" w14:textId="77777777" w:rsidR="009027BF" w:rsidRDefault="009027BF" w:rsidP="009027BF">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>Hello world</w:t>
            </w:r>
        </w:p>
        <w:p w14:paraId="6C84EB68" w14:textId="77777777" w:rsidR="009027BF" w:rsidRDefault="009027BF" w:rsidP="009027BF">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>Hello world</w:t>
            </w:r>
        </w:p>
        <w:p w14:paraId="4247D896" w14:textId="77777777" w:rsidR="009027BF" w:rsidRDefault="009027BF" w:rsidP="009027BF">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>Hello world</w:t>
            </w:r>
        </w:p>
        <w:p w14:paraId="594CCA3B" w14:textId="61C8A4A1" w:rsidR="009027BF" w:rsidRDefault="009027BF" w:rsidP="009027BF">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>Hello</w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>–</w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>world</w:t>
            </w:r>
        </w:p>
        <w:p w14:paraId="28E3E3B5" w14:textId="77777777" w:rsidR="009027BF" w:rsidRPr="00E14DC7" w:rsidRDefault="009027BF">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
        </w:p>
        <w:sectPr w:rsidR="009027BF" w:rsidRPr="00E14DC7">
            <w:pgSz w:w="11906" w:h="16838" />
            <w:pgMar w:top="1134" w:right="850" w:bottom="1134" w:left="1701" w:header="708" w:footer="708" w:gutter="0" />
            <w:cols w:space="708" />
            <w:docGrid w:linePitch="360" />
        </w:sectPr>
    </w:body>
</w:document>

console output:

output: " <w:t>world</w:"

The xml file in the example above is formatted. Outputs to the console without formatting

output: "r><w:t>world</w:"

How can I make the characterOffset method work fine? To appear in the console

output: "<w:t>world</w:>"

P.S. For a small xml file everything works fine

file-hello.xml

        <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14">
    <w:body>
        <w:p w14:paraId="4807E431" w14:textId="71C9FAEC" w:rsidR="00E14DC7" w:rsidRPr="00E14DC7" w:rsidRDefault="00E14DC7">
            <w:pPr>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:val="en-US" />
                </w:rPr>
                <w:t>Hello world</w:t>
            </w:r>
        </w:p>
        <w:sectPr w:rsidR="00E14DC7" w:rsidRPr="00E14DC7">
            <w:pgSz w:w="11906" w:h="16838" />
            <w:pgMar w:top="1134" w:right="850" w:bottom="1134" w:left="1701" w:header="708" w:footer="708" w:gutter="0" />
            <w:cols w:space="708" />
            <w:docGrid w:linePitch="360" />
        </w:sectPr>
    </w:body>
</w:document>

console output:

output: "<w:t>Hello world</w:t>"

Qt version 5.15.2 compiler MinGW 64bit

CodePudding user response:

Line 72 you have a multibyte character: U 2013 This character uses 3 bytes in UTF-8.

The function characterOffset counts it as a single character. However, when you read the file using seek and read you work with the numbers of bytes.

In you exemple the last element has a character offset of 4940, however it starts at the 4942th byte.

To make it work you need to use functions that handle properly multibyte characters. A solution is to use QString:

file.seek(0);
const QByteArray data = file.readAll();
const QString str = QString::fromUtf8(data);
qDebug() << str.mid(startPositionElem, endPositionElem - startPositionElem);

Another is to use QTextStream:

QTextStream stream(&file);
stream.seek(0);
stream.read(startPositionElem);
qDebug() << stream.read(endPositionElem - startPositionElem);

Note that in any case you cannot seek directly to a number of characters, only to a number of bytes. That means that you will have to read from the start of the file so that the function/class that do the unicode parsing can read and detect multibyte characters.

  • Related