Results 1 to 4 of 4

Thread: Strange error: doc.setContent(data) returns false

  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Strange error: doc.setContent(data) returns false

    Hi,
    I have a problem parsing some xml file from megaupload: my applycation, in short, extract links from a MegaUpload folder url. It works well but sometime I got error; but first of all the code:

    Qt Code:
    1. void Dialog::parseXml()
    2. {
    3. muitems.clear();
    4. if(!internetJob->isRequestAborted() && internetJob->getStatusCode() == 200)
    5. {
    6. QByteArray data = internetJob->getData();
    7. qDebug() << "Checking for data!!\n";
    8. if(data.isEmpty()) {
    9. qDebug() << "Megaupload returned an empty result!\n";
    10. return;
    11. }
    12.  
    13. if( !doc.setContent( data ) ) {
    14. qDebug() << "The XML obtained from Megaupload is invalid.";
    15. return;
    16. }
    17.  
    18. QDomElement root = doc.documentElement();
    19. if( root.tagName() != "FILES" ) {
    20. qDebug() << "The xml file invalid.";
    21. return;
    22. }
    23.  
    24. QDomNode n = root.firstChild();
    25. while( !n.isNull() )
    26. {
    27. QDomElement e = n.toElement();
    28. if( !e.isNull() )
    29. {
    30. if( e.tagName() == "ROW" )
    31. {
    32. MegaUploadItem mui;
    33. mui.name = e.attribute( "name", "" );
    34. mui.url = e.attribute( "url", "" );
    35. muitems.append(mui);
    36. }
    37. }
    38. n = n.nextSibling();
    39. }
    40. }
    41. qSort(muitems);
    42. }
    To copy to clipboard, switch view to plain text mode 

    Most of the times the code above runs good and my QVector muitems is filled with the needed data (file name and url); some times I got an error at doc.setContent(data) even if I dont' know why; in fact, if I comment line 16 parseXml() continues and data are retrieved correctly; other time I got a error in the same line but it is parsed only the first sibling (hoping I was clear). Below are three different urls: with the first I got no error; whit the second doc.setContent(data) returns false but commenting line 16 the data is retrieved correctly; while the third url gives the error plus only one iteration of while(!n.isNull()) {...}.

    http://www.megaupload.com/xml/folder...derid=D4HQHPLJ
    http://www.megaupload.com/xml/folder...derid=0JY6SVP1
    http://www.megaupload.com/xml/folder...derid=QEKO90W1

    You can copy these urls in your browser and take a look at the generated xml file. They are structurally the same...

    So my questions are:

    1) Why doc.setContent(data) returns false? How get a more verbose output?
    2) Why only the first tag is parsed and remaining not?

    I was thinking to use QRegExp for parsing, but the captured test is wrong: here a sample row of the xml file:

    <ROW name="VIDEO_TS.part06.rar" name_cut="VIDEO_TS.part06.rar" size="400 MB" url="http://www.megaupload.com/?d=3B1SORP1" downloadid = "3B1SORP1" sizeinbytes="419430400" expired="0"></ROW>
    and below my pattern:

    Qt Code:
    1. QRegExp pattern("<ROW\\s((\\w+)\\s*=\\s*(\"[^\"]\"))+></ROW>");
    To copy to clipboard, switch view to plain text mode 

    Probably the regular expression is wrong; who can review that?


    Best regards.
    Giuseppe CalÃ

  2. #2
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    5
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange error: doc.setContent(data) returns false

    Hello Giuseppe,

    I'm not going to comment on the problem you're having with XML, since I haven't used it enough to be knowledgeable in that area. I can comment on your regexp, however.

    To start with, using regular expressions for XML is not a good idea. They are far too brittle. If you choose to use one anyway, here is your corrected expression. I use a free program called Regex Coach to proofread mine. I didn't test this in Qt, but it should work.

    Qt Code:
    1. QRegExp pattern("<ROW\\s+((\\w+)\\s*=\\s*(\"[^\"]*\")\s*)+></ROW>");
    To copy to clipboard, switch view to plain text mode 
    Last edited by init2null; 8th January 2009 at 19:14.
    ImageRocket - My Qt4-based image editing program
    Project Page / Screenshots / Source

  3. #3
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Strange error: doc.setContent(data) returns false

    Quote Originally Posted by init2null View Post
    To start with, using regular expressions for XML is not a good idea. They are far too brittle. If you choose to use one anyway, here is your corrected expression. I use a free program called Regex Coach to proofread mine. I didn't test this in Qt, but it should work.

    Qt Code:
    1. QRegExp pattern("<ROW\\s+((\\w+)\\s*=\\s*(\"[^\"]*\")\s*)+></ROW>");
    To copy to clipboard, switch view to plain text mode 
    I have tested your pattern but it seems not working; it matches only the last couple of attributeName/attributeValue.

    I have used your pattern in the following piece of code:

    Qt Code:
    1. QTextStream ts(&data);
    2.  
    3. while (!ts.atEnd()) {
    4. QString line = ts.readLine();
    5. pattern.indexIn(line);
    6. int max = pattern.numCaptures() - 1;
    7. QStringList caps = pattern.capturedTexts();
    8. for ( int i = 1; i < max; i += 2 )
    9. {
    10. QString attributeName = caps.at(i);
    11. QString attributeValue = caps.at(i + 1);
    12.  
    13. // do something with that data now
    14. qDebug() << attributeName + "=" + attributeValue + "\n";
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    Thanks
    Giuseppe CalÃ

  4. #4
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    5
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange error: doc.setContent(data) returns false

    I guess regexps can only capture one value for each set of parenthesis. Since that's the case, either put in this snippet for each key-value pair
    Qt Code:
    1. (\\w+)\\s*=\\s*(\"[^\"]*\")\s*
    To copy to clipboard, switch view to plain text mode 
    or parse the XML. I looked a little at your initial questions, and I can answer your question on getting the XML error message. Use the optional arguments for the setContent method:
    Qt Code:
    1. bool QDomDocument::setContent ( const QString & text, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )
    To copy to clipboard, switch view to plain text mode 
    ImageRocket - My Qt4-based image editing program
    Project Page / Screenshots / Source

Similar Threads

  1. QSslSocke::supportSsl() returns false
    By oscar in forum Qt Programming
    Replies: 1
    Last Post: 9th September 2008, 19:51
  2. QSqlQuery::isValid returns false
    By mismael85 in forum Qt Programming
    Replies: 24
    Last Post: 8th September 2008, 00:43
  3. Problem with connect()
    By mrnor3 in forum Qt Programming
    Replies: 3
    Last Post: 23rd July 2008, 15:05
  4. Replies: 4
    Last Post: 8th July 2007, 15:26
  5. connect returns false
    By krivenok in forum Qt Programming
    Replies: 6
    Last Post: 21st February 2006, 21:01

Tags for this Thread

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.