Results 1 to 7 of 7

Thread: Reading values from XML

  1. #1
    Join Date
    May 2016
    Posts
    1
    Qt products
    Qt3
    Platforms
    Windows

    Default Reading values from XML

    How can i read this XML file in to lineedits of qt designer

    Qt Code:
    1. <studentform studentname="srkanth">
    2. <studentform address="1-66-1"/>
    3. <studentform ssc_marks="75%"/>
    4. <studentform inter_marks="82%"/>
    5. <studentform BTech_marks="65%"/>
    6. <studentform mail_id="srikanth.togara@gmail.com"/>
    7. </studentform>
    To copy to clipboard, switch view to plain text mode 


    And my code is below:
    Qt Code:
    1. QFile file(ui.file->text());
    2.  
    3. if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
    4. {
    5. qDebug( "Failed to open file for reading." );
    6. return;
    7. }
    8.  
    9. QDomDocument document;
    10. if( !document.setContent( &file ) )
    11.  
    12.  
    13. file.close();
    14.  
    15. QDomElement documentElement = document.documentElement();
    16.  
    17. QDomNode node = documentElement.firstChild();
    18. while( !node.isNull() )
    19. {
    20. if( node.isElement() )
    21. {
    22. QDomElement studentform = node.toElement();
    23.  
    24. ui.sri1->setText( studentform.tagName());
    25. ui.sri1->setText( studentform.attribute("studentname"));
    26.  
    27. ui.sri2->setText( studentform.tagName());
    28.  
    29. ui.sri2->setText( studentform.attribute( "address" ));
    30.  
    31. ui.sri3->setText( studentform.attribute( "ssc_marks"));
    32.  
    33. ui.sri4->setText( studentform.attribute("inter_marks"));
    34.  
    35. ui.sri5->setText( studentform.attribute( "BTech_marks" ));
    36.  
    37. ui.sri6->setText( studentform.attribute( "mail_id"));
    38. }
    39.  
    40. if( node.isText() )
    41. {
    42. QDomText text = node.toText();
    43.  
    44.  
    45. }
    46.  
    47. node = node.nextSibling();
    48.  
    49. }
    50.  
    51. return ;
    To copy to clipboard, switch view to plain text mode 



    output is looks like this:

    ui.sri1: srikanth
    ui.sri2:
    ui.sri3:
    ui.sri4:
    ui.sri5:
    ui.sri6:




    not getting data from lineedit 2 to 6 how can i fix this
    please help me.
    Last edited by anda_skoa; 8th June 2016 at 19:27. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading values from XML

    Your XML is not actually valid XML.
    Maybe post some of your actual data instead of trying to manually recreate something that you think looks like the actual data.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading values from XML

    Your XML is not actually valid XML.
    Well, it is valid XML, but it isn't good XML. What you have is an element "<studentform>" that contains 5 child "<studentform>" elements. Your code retrieves the parent (first "studentform" with the attribute "studentname"). That works. You then ask the same element for the values of its attributes "address", "ssc_marks", etc. which it does not have. These are attributes of the "studentform" children of the first node. So of course the GUI text fields are empty since the first node has no attributes by those names.

    Your code then asks for the siblings of the top-level node. It doesn't have any, so the loop exits. So, your code is doing exactly what you told it to do.

    You probably want your XML to look something like this:

    Qt Code:
    1. <studentform
    2. studentname="srkanth"
    3. address="1-66-1"
    4. ssc_marks="75%"
    5. inter_marks="82%"
    6. BTech_marks="65%"
    7. mail_id="srikanth.togara@gmail.com"
    8. />
    To copy to clipboard, switch view to plain text mode 

    and you don't want to loop over siblings, because then each sibling will overwrite the information from the previous sibling on your form.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading values from XML

    Quote Originally Posted by d_stranz View Post
    Well, it is valid XML, but it isn't good XML.
    Ah, indeed, I didn't see that the first studentform wasn't closed.
    I thought that we didn't see the document element and that the </studentform> was an unmachted close-tag.

    Still likely not the XML or the code that is actually being used, otherwise only "ui.sri6" would have a value, not "ui.sri1" as the poster claims.
    The claimed output would require a document element of sorts around the outer studentform tag to achieve that with the posted code.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading values from XML

    otherwise only "ui.sri6" would have a value, not "ui.sri1" as the poster claims.
    Well, not quite

    Presuming that the OP omitted the <?xml?> preamble and the document element, the C++ code as I read it says,

    Line 23 - get the first child of the document element (which in this case is the entire "XML" fragment in the original post, the outer <studentform> element and the <studentform> children contained within)

    Line 25 - set ui.sri1 to the tag name ("studentform"). I guess that didn't look right to the OP, so in the next line

    Line 26 - set ui.sri1 to the value of the attribute "studentname". This works, because the current (outer) element has such an attribute.

    Line 28 - set ui.sri2 to the tag name. That still doesn't look right I guess, so in the next line

    Line 30 - set ui.sri2 to the value of the attribute "address". Since the code is still looking at the outer element, it doesn't have any such attribute, so the returned string is empty.

    Lines 32, 34, 36, and 38 - Likewise, no such attributes in the outer element, so the ui.sriN fields are empty.

    Lines 41 - 46 - just for fun, if the node is a text node, get the text and then ignore it.

    Line 48 - get the next sibling of the current node. Since there is only one top-level <studentform> node, there is no sibling and the loop exits. As I said previously, if there was a sibling, the code would then proceed to overwrite everything from the previous iteration, with the end result that only the final <studentform> sibling's values would appear in the UI.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading values from XML

    Quote Originally Posted by d_stranz View Post
    Presuming that the OP omitted the <?xml?> preamble and the document element, the C++ code as I read it says,
    Well, true, under the assumption that there is a document element around the outer studentform.

    Which makes it even more important to get some info on the actual data.

    Which is unlikely to happen since the thread starter didn't respond to the first request in almost as week, so likely is not coming back at all.

    Cheers,
    _

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading values from XML

    so likely is not coming back at all
    The deadline to turn in the homework assignment has passed already.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Reading float values from binary file
    By shivendra46d in forum Newbie
    Replies: 0
    Last Post: 4th January 2016, 11:03
  2. Replies: 0
    Last Post: 22nd September 2015, 21:31
  3. Reading/writing long values to BLE device via Qt
    By WereWind in forum Qt Programming
    Replies: 0
    Last Post: 1st June 2015, 13:49
  4. Replies: 0
    Last Post: 9th March 2010, 13:02
  5. Reading values from file to array
    By Jeo_ in forum Newbie
    Replies: 0
    Last Post: 24th April 2009, 18:05

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.