Results 1 to 3 of 3

Thread: Cannot read second XML element

  1. #1
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Question Cannot read second XML element

    The question is:

    I have the following XML file which I believe is valid XML:

    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <Style>3</Style>
    3. <GameVariant>5</GameVariant>
    To copy to clipboard, switch view to plain text mode 

    and have the following code to read it:

    Qt Code:
    1. QString settingsFile = settingsDir + "/QBoardSettings.xml";
    2. QFile file(settingsFile);
    3. file.open(QIODevice::ReadOnly | QIODevice::Text);
    4. QXmlStreamReader xmlStreamReader(&file);
    5. while (xmlStreamReader.readNextStartElement()) {
    6. QString s = xmlStreamReader.name().toString();
    7. qDebug(qPrintable(s));
    8. }
    To copy to clipboard, switch view to plain text mode 

    But this code prints only "Style" instead of printing names for both elements. What I am doing wrong? Thanks in advance.

  2. #2
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Cannot read second XML element

    Just found answer - should have been using atEnd() and readNext().

  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: Cannot read second XML element

    Glad you got it working, but I believe the XML standards state that a valid XML document should be contained in a root element. In your example, something like the following where "Game" is the root element:
    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <Game>
    3. <Style>3</Style>
    4. <GameVariant>5</GameVariant>
    5. </Game>
    To copy to clipboard, switch view to plain text mode 
    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. go focus to The Next Element in qml
    By ravandi in forum Qt Quick
    Replies: 5
    Last Post: 13th November 2015, 10:04
  2. Replies: 3
    Last Post: 3rd May 2013, 21:27
  3. Replies: 9
    Last Post: 23rd April 2012, 14:53
  4. Read contents from the file using QHTTP Read()?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 21st June 2011, 09:03
  5. [QML] Video element
    By grezly in forum Qt Programming
    Replies: 1
    Last Post: 6th April 2011, 18:26

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.