开发者

How to get all attributes for a particular xml node in qt

is it possible to get all attributes for a particular node in pyqt ? for example .. consider for following node:

< asset Name="3d开发者_JS百科Asset" ID="5"/>

i want to retrieve the ("Name" and "ID") strings

is it possible?

thanks in advance


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.

Edit : Try this one.

With the QDomNamedNodeMap you are having give,

QDomNode QDomNamedNodeMap::item ( int index ) const

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

QDomAttr QDomNode::toAttr () const

With the QDomAttr obtained give,

QString name () const

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


How to get first attribute name/value in 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.


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.

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();
            }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜