Results 1 to 3 of 3

Thread: XML, SAX2, QXmlContentHandler::characters( const QString& ch ) problem

  1. #1
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default XML, SAX2, QXmlContentHandler::characters( const QString& ch ) problem

    Hi everybody !

    I'm developping a program that reads a XML file and gets its data using SAX2. It works quite well, the function QXmlContentHandler::characters(const QString& ch) of my handler is called for each content between element but also at each line end ... maybe it takes care of the carriage return and/or line feed specific character ? Does someone have the same problem ?

    Here is the content of my XML file (I have edited it using wordpad under Windows) :
    xml Code:
    1. <?xml version="1.0" encoding="ISO-8859-1"?>
    2. <phonebook>
    3. <person>
    4. <type>Worker</type>
    5. <name>NAME_1</name>
    6. <firstname>FIRST_NAME_1</firstname>
    7. <phone>0123456789</phone>
    8. </person>
    9. <person>
    10. <type>Student</type>
    11. <name>NAME_2</name>
    12. <firstname>FIRST_NAME_2</firstname>
    13. <phone>9876543210</phone>
    14. </person>
    15. </phonebook>
    To copy to clipboard, switch view to plain text mode 

    The code of my handler is :
    Qt Code:
    1. bool CMySaxContentHandler::startElement(const QString& namespaceURI, const QString& localName,
    2. const QString& qName, const QXmlAttributes& attribute)
    3. {
    4. szIndent += " ";
    5. szMessage = szIndent + "<" + qName + ">";
    6. qDebug("%s", szMessage.ascii());
    7.  
    8. return true;
    9. }
    10.  
    11. bool CMySaxContentHandler::endElement(const QString& namespaceURI, const QString& localName, const QString& qName)
    12. {
    13. szMessage = szIndent + "</" + qName + ">";
    14. qDebug("%s", szMessage.ascii());
    15. szIndent.remove(0, 4);
    16.  
    17. return true;
    18. }
    19.  
    20. bool CMySaxContentHandler::characters(const QString& ch)
    21. {
    22. szIndent += " ";
    23. szMessage = szIndent + ch;
    24. qDebug("%s", szMessage.ascii());
    25. szIndent.remove(0, 4);
    26.  
    27. return true;
    28. }
    To copy to clipboard, switch view to plain text mode 
    ... where szIndent and szMessage are two QString members.

    The code used to parse my XML file is the following :
    Qt Code:
    1. CMySaxContentHandler handler;
    2. xsr.setContentHandler(&handler);
    3. QXmlInputSource xis(QFile("test_without_dtd.xml"));
    4. xsr.parse(&xis);
    To copy to clipboard, switch view to plain text mode 

    and it produces the following output :
    xml Code:
    1. <phonebook>
    2.  
    3.  
    4. <person>
    5.  
    6.  
    7. <type>
    8. Worker
    9. </type>
    10.  
    11.  
    12. <name>
    13. NAME_1
    14. </name>
    15.  
    16.  
    17. <firstname>
    18. FIRST_NAME_1
    19. </firstname>
    20.  
    21.  
    22. <phone>
    23. 0123456789
    24. </phone>
    25.  
    26.  
    27. </person>
    28.  
    29.  
    30. <person>
    31.  
    32.  
    33. <type>
    34. Student
    35. </type>
    36.  
    37.  
    38. <name>
    39. NAME_2
    40. </name>
    41.  
    42.  
    43. <firstname>
    44. FIRST_NAME_2
    45. </firstname>
    46.  
    47.  
    48. <phone>
    49. 9876543210
    50. </phone>
    51.  
    52.  
    53. </person>
    54.  
    55.  
    56. </phonebook>
    To copy to clipboard, switch view to plain text mode 

    How could I know if the parameter of the characters handler function must be process if I am not sure it is an element content or something else ?

    Thanks for your help.
    Last edited by jacek; 5th October 2006 at 17:05. Reason: chaged [code] to [highlight] and wrapped too long line

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML, SAX2, QXmlContentHandler::characters( const QString& ch ) problem

    Maybe it will be enough if you check with a regexp whether ch contains only whitespace followed by '\n'?

  3. #3
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: XML, SAX2, QXmlContentHandler::characters( const QString& ch ) problem

    Ok, I have tryied what you adviced me and it is ok.

    I did :
    Qt Code:
    1. static int i = -1;
    2. i = ch.find( QRegExp("\n"), 0 );
    3.  
    4. if( i == -1 )
    5. {
    6. szIndent += " ";
    7. szMessage = szIndent + ch;
    8. qDebug("%s", szMessage.ascii());
    9. szIndent.remove(0, 4);
    10. }
    To copy to clipboard, switch view to plain text mode 

    and the output is now :
    Qt Code:
    1. <phonebook>
    2. <person>
    3. <type>
    4. Worker
    5. </type>
    6. <name>
    7. NAME_1
    8. </name>
    9. <firstname>
    10. FIRST_NAME_1
    11. </firstname>
    12. <phone>
    13. 0123456789
    14. </phone>
    15. </person>
    16. <person>
    17. <type>
    18. Student
    19. </type>
    20. <name>
    21. NAME_2
    22. </name>
    23. <firstname>
    24. FIRST_NAME_2
    25. </firstname>
    26. <phone>
    27. 9876543210
    28. </phone>
    29. </person>
    30. </phonebook>
    31. L
    To copy to clipboard, switch view to plain text mode 

    Thanks jacek

Similar Threads

  1. No QCalendarWidget in designer? 4.2.0-tp1
    By Spockmeat in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2006, 18:13
  2. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  3. Delegates and Table
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 18th May 2006, 19:47
  4. Replies: 4
    Last Post: 24th March 2006, 22:50
  5. Problem with custom widget
    By jnana in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2006, 11:55

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
  •  
Qt is a trademark of The Qt Company.