Results 1 to 10 of 10

Thread: XML parser problem

  1. #1
    Join Date
    Mar 2009
    Location
    Malaysia
    Posts
    25
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default XML parser problem

    Hi All,

    Below is my xml file content and my code to read it. How to read/write the MODVERS value in parameter tag? and

    ------------------------------------------------------------------------------------
    <?xml version="1.0" encoding="UTF-8"?>
    <communication CMD="03" OS="3" VER="1.0">
    <parameters>
    <parameter MODNAME="testing module" MODVERS="1.0.0.0"/>
    </parameters>
    </communication>
    -------------------------------------------------------------------------------------

    QFile file(strXMLPath);
    if(!QFile::exists(strXMLPath) || !file.open(QIODevice::ReadWrite) )
    {
    return;
    }
    //load the xml content
    QDomDocument doc;
    if(!doc.setContent(&file))
    {
    file.close();
    return;
    }
    file.close();
    QDomElement root = doc.documentElement();
    QDomElement item = root.firstChildElement();

    while(!item.isNull())
    {
    QString strTag = item.tagName();
    item = item.nextSiblingElement();
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: XML parser problem

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    Cantora (5th June 2009)

  4. #3
    Join Date
    Mar 2009
    Location
    Malaysia
    Posts
    25
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: XML parser problem

    For my current logic.. I am only able to read until <parameters>. Now I still fail to make my code can read until <parameter>

  5. #4
    Join Date
    Mar 2009
    Location
    Malaysia
    Posts
    25
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: XML parser problem

    so sorry.. after go thur the help file more details now i able to do it

    QDomElement root = doc.documentElement();
    QDomElement item = root.firstChildElement();
    QDomElement sub = item.firstChildElement();
    QString strTemp1 = sub.attribute("MODVERS");

  6. #5
    Join Date
    Mar 2009
    Location
    Malaysia
    Posts
    25
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: XML parser problem

    after I change the MODVERS value how I going to update the XML file?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: XML parser problem

    In firstChildElement() you can place the name of the tag you want to find. Don't rely on the order of tags...

    As for changes, there is QDomDocument::toString() that you can dump to a file afterwards.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Mar 2009
    Location
    Malaysia
    Posts
    25
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: XML parser problem

    so if I want to save it back to the same XML file then I need to clean the current file then after that only dump the new content into the file?

    QString xml = doc.toString();

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: XML parser problem

    You don't need to clean it. Just open the file in read/write mode and the contents will be truncated.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Mar 2009
    Location
    Malaysia
    Posts
    25
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: XML parser problem

    after i update my xml file. the file content become like below:
    -----------------------------------------------
    <?xml version='1.0' encoding='UTF-8'?>
    <communication OS="3" CMD="03" VER="1.0" >
    <parameters>
    <parameter MODNAME="iCMDataCard" MODVERS="1.0.1.2" />
    </parameters>
    </communication>
    -1ion>
    ------------------------------------------------

    the file is not able to open is my vs 2005. got one warning message "Inconsistent line endings"



    QDomElement sub = item.firstChildElement("parameter");
    QString strTemp1 = sub.attribute("MODVERS");
    sub.setAttribute("MODVERS", "1.0.1.2");
    strTemp1 = sub.attribute("MODVERS");

    file.open(QIODevice::ReadWrite);
    QTextStream out(&file);
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QString strUTF8 = codec->toUnicode(doc.toString().toLocal8Bit());
    out << strUTF8;
    out << EOF;
    file.close();

  11. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: XML parser problem

    try this code
    Qt Code:
    1. ...
    2. doc.appendChild(doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\""));
    3. QDomElement root = doc.createElement("communication");
    4. root.setAttribute("OS", 3);
    5. root.setAttribute("CMD", "03");
    6. root.setAttribute("VER", 1.0);
    7. QDomElement parameters = doc.createElement("parameters");
    8. QDomElement parameter = doc.createElement("parameter");
    9. parameter.setAttribute("MODNAME", "iCMDataCard");
    10. parameter.setAttribute("MODVERS", "1.0.1.2");
    11. parameters.appendChild(parameter);
    12. root.appendChild(parameters);
    13. doc.appendChild(root);
    14.  
    15. QFile file("settings.xml");
    16. if (file.open(QIODevice::WriteOnly)) {
    17. QTextStream out(&file);
    18. out << doc.toString();
    19. }
    20. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. Replies: 1
    Last Post: 23rd April 2009, 09:05
  2. Problem in using QHttp with QTimer
    By Ferdous in forum Newbie
    Replies: 2
    Last Post: 6th September 2008, 12:48
  3. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  4. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.