1

我需要在 Qt 中解析 .vcproj 文件,这个文件实际上是 xml 文件和看起来像:

<?xml version="1.0" encoding="windows-1251"?>
<VisualStudioProject...>
    <Platforms>
        <Platform
            Name="Win32"
        />
    </Platforms>
    <ToolFiles>
    </ToolFiles>
    <Configurations>
        <Configuration
            Name="Debug|Win32"
            OutputDirectory="$(SolutionDir)$(ConfigurationName)"
            IntermediateDirectory="$(ConfigurationName)"
            ConfigurationType="1"
            CharacterSet="2"
            >
            ...
        </Configuration>
        <Configuration
            Name="Release|Win32"
            OutputDirectory="$(SolutionDir)$(ConfigurationName)"
            IntermediateDirectory="$(ConfigurationName)"
            ConfigurationType="1"
            CharacterSet="2"
            WholeProgramOptimization="1"
            >
            ...
        </Configuration>
    </Configurations>
    <References>
    </References>
    <Files>
        <Filter
            Name="Source Files"
            Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
            UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
            >
            ...
        </Filter>
        <Filter
            Name="CppOp"
            >
            ...
        </Filter>
    </Files>
    <Globals>
    </Globals>
</VisualStudioProject>

我需要获取所有配置名称,我使用这个:

QFile* file = new QFile(xmlFile);
    if (file->open(QIODevice::ReadOnly | QIODevice::Text)) {
        QXmlStreamReader xml(file);
        while (!xml.atEnd() && !xml.hasError()){
            QXmlStreamReader::TokenType token = xml.readNext();
            if (token == QXmlStreamReader::StartDocument)
                continue;
            if (token == QXmlStreamReader::StartElement) {
                if (xml.name() == "Configurations")
                    continue;
                if (xml.name() == "Configuration") {
                    QXmlStreamAttributes attributes = xml.attributes();
                    if (attributes.hasAttribute("Name"))
                        ui->prefferedConfigSelectComboBox->addItem(attributes.value("Name").toString());
                }
            }
        }
    }

我尝试使用http://developer.nokia.com/Community/Wiki/QXmlStreamReader_to_parse_XML_in_Qt。看起来 QXmlStreamReader 没有“看到”此文件中的 Name 属性。但它不起作用,我不知道为什么。

4

0 回答 0