Results 1 to 3 of 3

Thread: Reading a xml file and parsing it using DOM parser

  1. #1
    Join Date
    Nov 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Reading a xml file and parsing it using DOM parser

    I am not that good with xml, but my basic xml file looks somthing like this.

    Qt Code:
    1. <MAIN_HEADER>
    2.  
    3. <HEADER>
    4. <TITLE>my_title</TITLE>
    5. <AUTOR>DNL</AUTOR>
    6. <NAME>John</NAME>
    7. <AGE>abc</AGE>
    8. <SEX>male</SEX>
    9. <PLACE>abc</PLACE>
    10. <INI_FILE>abc</INI_FILE>
    11.  
    12. </HEADER>
    To copy to clipboard, switch view to plain text mode 

    what I want to do is, I need to find 2-3 tags, say for example NAME & SEX and store the attribute(John,Male) in another variable.

    until now, I have been able to make it read the xml file.

    Qt Code:
    1. void MainWindow::XMLParser()
    2. {
    3. QString path=MainWindow::getWorkingDirectory()+"\\0_Config\\";
    4. QString string;
    5. string = path + ui->ConfigFiles_combo->currentText(); \\THIS IS WHERE´IT DETERMINES WHICH XML FILE IT IS
    6. qDebug()<<string;
    7. QDomDocument document;
    8. //load the file
    9. QFile file(string);
    10. if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    11. {
    12. qDebug()<<"Failed to open the file";
    13.  
    14. }
    15.  
    16. else
    17. {
    18. if(!document.setContent(false))
    19. {
    20. qDebug()<<"Failed to load document";
    21.  
    22. }
    23. file.close();
    24. }
    25. QDomElement root = document.firstChildElement();
    26. qDebug()<<"finished";
    27.  
    28. }
    To copy to clipboard, switch view to plain text mode 

    how do I make it search for the exact tag and store it inside another variable?

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

    Default Re: Reading a xml file and parsing it using DOM parser

    Your setContent() line is wrong, it is not getting the file.

    Anyway, QDomDocument::elementsByTagName().

    Cheers,
    _

  3. #3
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Reading a xml file and parsing it using DOM parser

    The example you show isn't valid XML. It should have a closing tag for <MAIN_HEADER>, as in </MAIN_HEADER>.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Replies: 2
    Last Post: 13th July 2014, 03:09
  2. parsing ini file in qt using custom parser
    By akshaysulakhe in forum Qt Programming
    Replies: 2
    Last Post: 9th July 2013, 23:25
  3. Problem in editing xml file through Dom parser.
    By Niamita in forum Qt Programming
    Replies: 5
    Last Post: 20th September 2011, 09:02
  4. problem in reading xml file with SAX parser.
    By Niamita in forum Qt Programming
    Replies: 11
    Last Post: 14th September 2011, 10:13
  5. Which parser is better for edit xml file.
    By Niamita in forum Qt Programming
    Replies: 2
    Last Post: 13th September 2011, 07:27

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.