2

I am trying to read an xml file in Qt, which I successfully generated using a different method. Here is my xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Project>
    <EditorTheme>NULL</EditorTheme>
    <Modules>
        <Module>
            <Name>Module_Renderer</Name>
            <Position>471,164</Position>
            <Size>200,100</Size>
            <Locked>true</Locked>
            <Visible>true</Visible>
        </Module>
        <Module>
            <Name>Module_Console</Name>
            <Position>200,229</Position>
            <Size>256,192</Size>
            <Locked>true</Locked>
            <Visible>false</Visible>
        </Module>
        <Module>
            <Name>Module_ResourceToolkit</Name>
            <Position>1049,328</Position>
            <Size>200,100</Size>
            <Locked>true</Locked>
            <Visible>true</Visible>
        </Module>
        <Module>
            <Name>Module_CellEditor</Name>
            <Position>542,564</Position>
            <Size>200,100</Size>
            <Locked>true</Locked>
            <Visible>false</Visible>
        </Module>
    </Modules>
</Project>

And here is some code that I am using to parse this file:

Project ProjectLoader::loadLastProject( ConsoleModule* console ) {
    Project project;
    // load xml
    QFile file( "C:/Users/Krynn/Desktop/LastProject.xml" );
    if( !file.open( QFile::ReadOnly | QFile::Text ) ) {
        // print error cannot open
    }
    QXmlStreamReader reader;
    console->outputDisplay->append( "Test" );
    reader.setDevice( &file );
    reader.readNext();
    while( !reader.atEnd() && !reader.hasError() ) {
        reader.readNext();
        if( reader.isStartElement() ) {
            QString name = reader.name().toString();
            if( reader.name() == "Project" ) {
                reader.readNextStartElement();
                if( reader.name().toString() == "EditorTheme" ) {
                    // Append Project theme
                    console->outputDisplay->append( "Theme Detected: " + reader.name().toString() + " " + reader.readElementText() );
                }
                reader.readNextStartElement();
                if( reader.name().toString() == "Modules" ) {
                    // how do I proceed??
                    console->outputDisplay->append( QString( "" ) + " " + reader.name().toString() + " " + reader.readElementText() );
                }

            }
        }
    }
    if( reader.hasError() ) {
        console->outputDisplay->append( "XML error: " + reader.errorString() );
    } else if( reader.atEnd() ) {
        console->outputDisplay->append( "End of XML File Reached" );
    }
    file.close();
    return project;
}

And here is some visual output for what that code gives me: enter image description here

Really, I just don't know how I would go about loading all the module data within the xml file. I was using a plain text file previously to store all this stuff, but now I want to upgrade. Any help would be greatly appreciated.

1 Answer 1

3

Nevermind I figured it out.

Project ProjectLoader::loadLastProject( ConsoleModule* console ) {
    Project project;
    // load xml
    QFile file( "C:/Users/Krynn/Desktop/LastProject.xml" );
    if( !file.open( QFile::ReadOnly | QFile::Text ) ) {
        // print error cannot open
    }
    QXmlStreamReader reader;
    reader.setDevice( &file );
    reader.readNext();
    int count = 0;
    while( !reader.atEnd() ) { //&& !reader.hasError()
        reader.readNext();
        if( reader.isStartElement() ) {
            if( reader.name().toString() == "Module" ) {
                WindowModuleSaveData data;
                reader.readNextStartElement();
                data.name = reader.readElementText(); // name
                reader.readNextStartElement();
                data.position = convertStringToQPoint( reader.readElementText() );
                console->outputDisplay->append( convertQPointToString(data.position) );
                reader.readNextStartElement();
                data.size = convertStringToQSize( reader.readElementText() );
                reader.readNextStartElement();
                data.isLocked = reader.readElementText() == "true" ? true : false;
                reader.readNextStartElement();
                data.isVisible = reader.readElementText() == "true" ? true : false;
                project.modules.push_back( data );
                console->outputDisplay->append("Loaded A Module");
            }
            count++;
        }
    }
    console->outputDisplay->append( QString::number( count ) );
    if( reader.hasError() ) {
        console->outputDisplay->append( "XML error: " + reader.errorString() );
    } else if( reader.atEnd() ) {
        console->outputDisplay->append( "End of XML File Reached" );
    }
    file.close();
    return project;
}

The above code may be error prone, because it assumes what the next child may be instead of actually testing for it. Good enough for now though.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.