Results 1 to 4 of 4

Thread: Mysterious QXMLStreamReader parsing behavior

  1. #1
    Join Date
    May 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Mysterious QXMLStreamReader parsing behavior

    Hi,all,

    I am new to Qt xml classes.
    When reading XmlStreamReader example from the book by Jasmin Blanchette, I encountered mysterious QXMLStreamReader parsing behavior.
    The small xml file from the book is listed as following:

    <?xml version="1.0"?>
    <bookindex>
    <entry term="sidebearings">
    <page>10</page>
    <page>34-35</page>
    <page>307-308</page>
    </entry>
    <entry term="subtraction">
    <entry term="of pictures">
    <page>115</page>
    <page>244</page>
    </entry>
    <entry term="of vectors">
    <page>9</page>
    </entry>
    </entry>
    </bookindex>
    My question is: Why do so many "Characters" Tokens emerge during the parsing procedure?
    As an example, for line "<page>307-308</page>", after readElementText(),Token is "EndElement", with another readNext(), "</entry>" should be read, and the token is supposed to be "EndElement", but "Characters" token appeared, Why?

    Thanks for your help.


    All the tokens reported by the parser are listed below:
    StartDocument
    StartElement
    Characters
    StartElement
    Characters
    StartElement
    EndElement
    Characters
    StartElement
    EndElement
    Characters
    StartElement
    EndElement
    Characters
    EndElement
    Characters
    StartElement
    Characters
    StartElement
    Characters
    StartElement
    EndElement
    Characters
    StartElement
    EndElement
    Characters
    EndElement
    Characters
    StartElement
    Characters
    StartElement
    EndElement
    Characters
    EndElement
    Characters
    EndElement
    Characters
    EndElement
    EndDocument

  2. #2
    Join Date
    Oct 2007
    Posts
    21
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Mysterious QXMLStreamReader parsing behavior

    Hello Friend,

    I can't say exactly, but it might be a whitespace, i think the parser consider whitespace as "Characters".
    so just check for whitespace before you read next element...

    try this to check whitespace..!!

    Qt Code:
    1. QXmlStreamReader *reader = new QXmlStreamReader();
    2. if( reader->isWhitespace() == true )
    3. {
    4. reader->readNext();
    5. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Mysterious QXMLStreamReader parsing behavior

    Thanks for your help.

    I'll try to understand the example code with WhiteSpace and NewLine in mind.

  4. #4
    Join Date
    Apr 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Mysterious QXMLStreamReader parsing behavior

    Hi,
    How about this xml document. Do you have any idea how to parse it using QXml StreamReader or QDomDocument??

    <p>
    There are two types of hydraulic tools for the connecting rod shank screws, see
    <ref xml:link="simple" inline="true" behavior="external" content-role="fig" href="fig-400731-low">Fig 07-20</ref>
    . The screws and nuts in the tool of
    <hp0>new design</hp0>
    (introduced in year 2001) should be replaced before reaching
    <hp0>1000</hp0>
    loading cycles, i.e raising the pressure to nominal value 1000 times).
    </p>

    Any help please. I have to read all of them.
    I am using things like this:
    QString content=reader.readElementText(QXmlStreamReader::I ncludeChildElements);
    it will include everything inside <p> tag including those ref hp0. Then, I will go back to the beginning of <p> tag and readNext() to read the attribute and value <ref> and <hp0> tag.

    while(!reader.isEndElement()||reader.name()!="p"){
    if(reader.isStartElement()){
    if(reader.name()=="ref"){
    QString displaytext=reader.readElementText();
    QXmlStreamAttributes attrs = reader.attributes();
    QString role=attrs.value("content-role").toString();
    QString href=attrs.value("href").toString();
    QString link="<a href=\""+role+";"+href+"\">["+displaytext+"]</a>";
    value.replace(displaytext,link);
    }else if(reader.name()=="hp0"){
    QString bold=reader.readElementText();
    int bindex=value.indexOf(bold);
    if(bindex!=-1&&value.indexOf("<b>"+bold+"</b>")==-1){
    value.replace(bindex,bold.length(),"<b>"+bold+"</b>");
    }
    }else{
    qDebug()<<"error. Undefined tag name inside <p>";
    qDebug()<<reader.name();
    }
    }
    reader.readNext();
    }

    HOWEVER, my big and funny problem is that with QXmlStreamReader we dont have the option to roll back to previous start tag. In this case when I use
    QString content=reader.readElementText(QXmlStreamReader::I ncludeChildElements);
    the current cursor is at the end of tab </p> and I can not roll back to the beginning of <p> tag to read <fig> and <hp0> tag
    Any help please.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.