简体   繁体   English

从QDomDocument提取属性值

[英]Extracting attribute values from a QDomDocument

I have an xml block that I need to search through to retrieve a particular attribute's value using QDomDocument. 我有一个XML块,我需要使用QDomDocument进行搜索以检索特定属性的值。 At present that attribute can unfortunately either be in a tag called 'panel' or alternatively 'panel-config'. 目前,不幸的是,该属性可以位于名为“ panel”的标签中,也可以位于“ panel-config”中。 To add further complexity often 'panel' can be the root node of the document. 为了增加进一步的复杂性,通常“面板”可以成为文档的根节点。 Anyway at present my code needs to do this: 无论如何,目前我的代码需要这样做:

QDomElement element = XML.documentElement();

if ((element.tagName() == "panel") && (element.hasAttribute(sFACETPANELID))) {
    panelId = element.attribute(sFACETPANELID);
}
else
{
    // Get all our panel nodes
    QDomNodeList nodes = element.elementsByTagName("panel");

    // Loop through our nodes and extract our paned id
    for (int i = 0; i < nodes.count(); ++i) {
        QDomElement panel = nodes.at(i).toElement();

        if (panel.hasAttribute(sFACETPANELID)) {
            panelId = panel.attribute(sFACETPANELID);
            break;
        }
    }   
}

Followed by this: 其次是:

QDomNodeList nodes = element.elementsByTagName("panel_config");

for (int i = 0; i < nodes.count(); ++i) {
    QDomElement panel_config = nodes.at(i).toElement();

    if (panel_config.hasAttribute(sFACETCLUSTERID)) {
        clusterId = panel_config.attribute(sFACETCLUSTERID);
        break;
    }
}   

This is pretty horrible. 这太可怕了。 What I ideally require is a way to retrieve a nodeList() for any tags that match either 'panel' or 'panel-config' (elementsByTagNames if you will) and iterate through that list in one single loop. 我理想地需要的是一种检索与“ panel”或“ panel-config”(如果需要的话为elementsByTagNames)匹配的任何标签的nodeList()的方法,并在一个循环中迭代该列表。 I'd also prefer not to have to check if the top level element is called 'panel' before attempting to get the elementsByTagName. 我也不想在尝试获取elementsByTagName之前不必检查顶级元素是否称为“ panel”。 Any suggestions if this can be done in QT? 有什么建议可以在QT中完成吗?

Edit: previous code deleted 编辑:先前的代码已删除

For lists of elements use 对于元素列表,请使用

//QDomDocument.elementsByTagName(...)
QDomNodeList panelList = XML.elementsByTagName("panel")
QDomNodeList panelCfgList = XML.elementsByTagName("panel-config")

it will match the root element too. 它也将匹配根元素。 Unfortunately you can't directly change (or add) nodes in QDomNodeList, so you'll have to make QList that contains both lists and loop through it, or make loop like this 不幸的是,您无法直接更改(或添加)QDomNodeList中的节点,因此您必须制作包含列表的QList并循环遍历,或者像这样进行循环

for (int i = 0; i < panelList.count() || i < panelCfgList.count(); i++)
{
    if (i < panelList.count())
    {
        QDomElement panel = panelList.at(i).toElement();
        if (panel.hasAttribute(sFACETPANELID))
        {
            panelId = panel.attribute(sFACETPANELID);
            break;
        }
    }
    if (i < panelCfgList.count())
    {
        QDomElement panelCfg = panelCfgList.at(i).toElement();
        if (panelCfg.hasAttribute(sFACETPANELID))
        {
            panelCfgId = panelCfg.attribute(sFACETPANELID);
            break;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM