如何在qt中获取特定xml节点的所有属性

时间:2022-12-08 07:59:26

is it possible to get all attributes for a particular node in pyqt ? for example .. consider for following node:
< asset Name="3dAsset" ID="5"/>
i want to retrieve the ("Name" and "ID") strings
is it possible?

是否可以获取pyqt中特定节点的所有属性?例如. .考虑以下节点:< asset Name="3dAsset" ID="5"/>我想检索("Name"和"ID")字符串,可能吗?

thanks in advance

谢谢提前

3 个解决方案

#1


7  

You can retrieve the particular value of the attribute using the function,

您可以使用函数检索属性的特定值,

QString QDomElement::attribute ( const QString & name, const QString & defValue = QString() ) const

To get all the attributes use,

要得到所有属性,

QDomNamedNodeMap QDomElement::attributes () const

and you have to traverse through the DomNamedNodeMap and get the value of each of the attributes. Hope it helps.

您必须遍历DomNamedNodeMap并获取每个属性的值。希望它可以帮助。

Edit : Try this one.

编辑:试试这个。

With the QDomNamedNodeMap you are having give,

有了QDomNamedNodeMap,

QDomNode QDomNamedNodeMap::item ( int index ) const

which will return a QDomNode for the particular attribute. Then give,

它将返回特定属性的QDomNode。然后给,

QDomAttr QDomNode::toAttr () const

With the QDomAttr obtained give,

用QDomAttr获得give,

QString name () const

which will return the name of the attribute. Hope it helps.

它将返回属性的名称。希望它可以帮助。

#2


2  

How to get first attribute name/value in PySide/PyQt:

如何获得PySide/PyQt中的第一个属性名/值:

if node.hasAttributes():
    nodeAttributes = node.attributes()
    attributeItem = nodeAttributes.item(0) #pulls out first item
    attribute = attributeItem.toAttr()
    attributeName = attr.name()
    attributeValue = attr.value()

This just shows how to get one name/value pair, but it should be easy enough to extend looping with nodeAttributes.length() or something similar.

这只是展示了如何获得一个名称/值对,但是使用nodeAttributes.length()或类似的东西扩展循环应该足够简单。

#3


0  

This is for c++. I Ran into the same problem. You need to convert to QDomAttr. I'm sure API is the same in python.

这是c++。我遇到了同样的问题。您需要转换到QDomAttr。我确信在python中API是一样的。

if( Node.hasAttributes() )
{
    QDomNamedNodeMap map = Node.attributes();
    for( int i = 0 ; i < map.length() ; ++i )
    {
        if(!(map.item(i).isNull()))
        {
            QDomNode debug = map.item(i);
            QDomAttr attr = debug.toAttr();
            if(!attr.isNull())
            {
                cout << attr.value().toStdString();
                cout << attr.name().toStdString();
            }
    }
}

#1


7  

You can retrieve the particular value of the attribute using the function,

您可以使用函数检索属性的特定值,

QString QDomElement::attribute ( const QString & name, const QString & defValue = QString() ) const

To get all the attributes use,

要得到所有属性,

QDomNamedNodeMap QDomElement::attributes () const

and you have to traverse through the DomNamedNodeMap and get the value of each of the attributes. Hope it helps.

您必须遍历DomNamedNodeMap并获取每个属性的值。希望它可以帮助。

Edit : Try this one.

编辑:试试这个。

With the QDomNamedNodeMap you are having give,

有了QDomNamedNodeMap,

QDomNode QDomNamedNodeMap::item ( int index ) const

which will return a QDomNode for the particular attribute. Then give,

它将返回特定属性的QDomNode。然后给,

QDomAttr QDomNode::toAttr () const

With the QDomAttr obtained give,

用QDomAttr获得give,

QString name () const

which will return the name of the attribute. Hope it helps.

它将返回属性的名称。希望它可以帮助。

#2


2  

How to get first attribute name/value in PySide/PyQt:

如何获得PySide/PyQt中的第一个属性名/值:

if node.hasAttributes():
    nodeAttributes = node.attributes()
    attributeItem = nodeAttributes.item(0) #pulls out first item
    attribute = attributeItem.toAttr()
    attributeName = attr.name()
    attributeValue = attr.value()

This just shows how to get one name/value pair, but it should be easy enough to extend looping with nodeAttributes.length() or something similar.

这只是展示了如何获得一个名称/值对,但是使用nodeAttributes.length()或类似的东西扩展循环应该足够简单。

#3


0  

This is for c++. I Ran into the same problem. You need to convert to QDomAttr. I'm sure API is the same in python.

这是c++。我遇到了同样的问题。您需要转换到QDomAttr。我确信在python中API是一样的。

if( Node.hasAttributes() )
{
    QDomNamedNodeMap map = Node.attributes();
    for( int i = 0 ; i < map.length() ; ++i )
    {
        if(!(map.item(i).isNull()))
        {
            QDomNode debug = map.item(i);
            QDomAttr attr = debug.toAttr();
            if(!attr.isNull())
            {
                cout << attr.value().toStdString();
                cout << attr.name().toStdString();
            }
    }
}