Results 1 to 3 of 3

Thread: Serialize class objects to XML file and back

  1. #1
    Join Date
    Jan 2012
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Serialize class objects to XML file and back

    Hi,

    I have two classes:

    Qt Code:
    1. class A
    2. {
    3. public:
    4. int m_x;
    5. string m_s;
    6. };
    7.  
    8. class B
    9. {
    10. public:
    11. A m_a;
    12. };
    To copy to clipboard, switch view to plain text mode 

    Now I would like to create a XML structure, like this one:

    Qt Code:
    1. <B>
    2. <A>
    3. <x>12</x>
    4. <y>55</y>
    5. </A>
    6. </B>
    To copy to clipboard, switch view to plain text mode 

    What is the best way to achieve this with Qt?

    It would be necessary to read the XML and convert it to the object model later.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Serialize class objects to XML file and back

    Add an serializer operator in each class.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: Serialize class objects to XML file and back

    Quote Originally Posted by StarShaper View Post
    Hi,

    I have two classes:

    Qt Code:
    1. class A
    2. {
    3. public:
    4. int m_x;
    5. string m_s;
    6. };
    7.  
    8. class B
    9. {
    10. public:
    11. A m_a;
    12. };
    To copy to clipboard, switch view to plain text mode 

    Now I would like to create a XML structure, like this one:

    Qt Code:
    1. <B>
    2. <A>
    3. <x>12</x>
    4. <y>55</y>
    5. </A>
    6. </B>
    To copy to clipboard, switch view to plain text mode 

    What is the best way to achieve this with Qt?

    It would be necessary to read the XML and convert it to the object model later.
    How do you handle this:


    Qt Code:
    1. class A
    2. {
    3. public:
    4. int m_x;
    5. string m_s;
    6. };
    7.  
    8. class B
    9. {
    10. public:
    11. A m_a1;
    12. A m_a2
    13. };
    To copy to clipboard, switch view to plain text mode 
    ?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. problem using gui objects after leving the main class
    By ruben.rodrigues in forum Newbie
    Replies: 7
    Last Post: 17th June 2010, 17:43
  2. Instantiate objects only having their class name
    By victor.fernandez in forum Qt Programming
    Replies: 1
    Last Post: 30th June 2009, 16:22
  3. Replies: 0
    Last Post: 25th June 2009, 08:17
  4. Replies: 1
    Last Post: 25th July 2008, 23:44
  5. using class objects globally??????
    By pratik in forum Qt Programming
    Replies: 2
    Last Post: 9th July 2007, 13:51

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.