Qt读取XML文件

时间:2014-12-02 00:53:15

标签: c++ xml qt

我正在尝试读取Qt中的xml文件,我使用不同的方法成功生成了该文件。这是我的xml文件:

<?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>

以下是我用来解析此文件的一些代码:

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;
}

这是代码给我的一些视觉输出: enter image description here

真的,我只是不知道如何在xml文件中加载所有模块数据。我以前使用纯文本文件存储所有这些东西,但现在我想升级。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

没关系我弄清楚了。

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;
}

上面的代码可能容易出错,因为它假设下一个孩子可能是什么而不是实际测试它。现在已经足够了。