1

I created this xml file with QXmlStreamWriter:

<?xml version="1.0" encoding="UTF-8"?>
<Draw>
    <Input>
        <Column title="A"/>
        <Column title="B"/>
        <Column title="C"/>
        <Column title="D">
            <item id="0">Bayer Leverkusen</item>
            <item id="1">Benfica</item>
            <item id="2">Villareal</item>
            <item id="3">Montpellier</item>
        </Column>
    </Input>
</Draw>

I would like to create a Vector of String containing all the items inside the tag Column title="D": Now, I know how to create a QVector and how they fit elements on the inside, I just have to figure out how I can do this by extrapolating information from an xml file.

Can you help me?

4

2 回答 2

0

您可以使用QXmlStreamReader来遍历 XML 元素并找到该<Column title="D">元素。一旦找到它,就可以使用readNextStartElement()in 组合来读取它的所有子元素。skipCurrentElement()

假设您在示例中显示的 XML 文档可以从xmlDocument对象中读取。<item>要使用适当的错误检查从元素中提取所有元素<Column title="D">,您可以执行以下操作:

QXmlStreamReader xmlIterator(xmlDocument);
QVector<QString> output;

for(; !xmlIterator.atEnd(); xmlIterator.readNext()) {
    if(isStartElementOfColumnD(xmlIterator)) {
        while(xmlIterator.readNextStartElement()) {
            if(isItemElement(xmlIterator))
                output.append(xmlIterator.readElementText());
            else
                xmlIterator.skipCurrentElement();
        }
    }
}

if(xmlIterator.hasError())
    qCritical() << "Error has occurred:" << xmlIterator.errorString();
else
    qDebug() << output;

在上面的示例中,我使用了两个谓词来隐藏xmlIterator. 这些是:

inline bool isStartElementOfColumnD(const QXmlStreamReader& xmlIterator) {
    return xmlIterator.isStartElement() && xmlIterator.name() == "Column" &&
            xmlIterator.attributes().value("title") == "D";
}
inline bool isItemElement(const QXmlStreamReader& xmlIterator) {
    return xmlIterator.name() == "item" &&
            xmlIterator.attributes().hasAttribute("id");
}

样本结果:

QVector("Bayer Leverkusen", "Benfica", "Villareal", "Montpellier")
于 2017-11-23T09:03:34.827 回答
-1

我会用以下方式写它:

QVector<QString> store;

[..]
if (reader.readNextStartElement() && reader.name() == "Draw") {
    while (reader.readNextStartElement() && reader.name() == "Input") {
        while (reader.readNextStartElement()) {
            QXmlStreamAttributes attr = reader.attributes();
            if (reader.name() == "Column" && attr.value("title").toString() == "D") {
                while(!(reader.isEndElement() && reader.name() == "Column")) {
                    if (reader.isStartElement() && reader.name() == "item") {
                        QString text = reader.readElementText();
                        store.append(text);
                    }
                    reader.readNext();
                    if (reader.hasError()) {
                        // Handle error.
                        QString msg = reader.errorString();
                        break;
                    }
                }
            } else {
                reader.readNext();
            }
        }
    }
} else {
    reader.raiseError("Expected <Draw> element");
}
[..]
于 2014-02-17T13:30:33.973 回答