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

Python 如何在qt中获取特定xml节点的所有属性,python,xml,qt,pyqt,Python,Xml,Qt,Pyqt,是否可以获取pyqt中特定节点的所有属性? 例如考虑以下节点: 这是C++的。我遇到了同样的问题。您需要转换为QDomAttr。我确信API在python中是相同的 if( Node.hasAttributes() ) { QDomNamedNodeMap map = Node.attributes(); for( int i = 0 ; i < map.length() ; ++i ) { if(!(map.item(i).isNull()))

是否可以获取pyqt中特定节点的所有属性? 例如考虑以下节点:


我想检索(“Name”和“ID”)字符串
可能吗


提前感谢

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

QString QDomElement::attribute ( const QString & name, const QString & defValue = QString() ) const
要获得使用的所有属性

QDomNamedNodeMap QDomElement::attributes () const
您必须遍历DomNamedNodeMap并获得每个属性的值。希望能有帮助

编辑:试试这个

有了QDomNamedNodeMap,你就有了give

QDomNode QDomNamedNodeMap::item ( int index ) const
它将返回特定属性的QDomNode。 然后给,

QDomAttr QDomNode::toAttr () const
在获得QDomAttr后

QString name () const
它将返回属性的名称。
希望有帮助。

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

QString QDomElement::attribute ( const QString & name, const QString & defValue = QString() ) const
要获得使用的所有属性

QDomNamedNodeMap QDomElement::attributes () const
您必须遍历DomNamedNodeMap并获得每个属性的值。希望能有帮助

编辑:试试这个

有了QDomNamedNodeMap,你就有了give

QDomNode QDomNamedNodeMap::item ( int index ) const
它将返回特定属性的QDomNode。 然后给,

QDomAttr QDomNode::toAttr () const
在获得QDomAttr后

QString name () const
它将返回属性的名称。
希望它有帮助。

< P>这是C++的。我遇到了同样的问题。您需要转换为QDomAttr。我确信API在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();
            }
    }
}
if(Node.hasAttributes())
{
QDomNamedNodeMap=Node.attributes();
对于(int i=0;iCUT< P>这是C++的。我遇到了同样的问题。你需要转换成QDomAttr。我确信Python中API是相同的。< /P>
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();
            }
    }
}
if(Node.hasAttributes())
{
QDomNamedNodeMap=Node.attributes();
对于(int i=0;icout如何在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()

这只是显示了如何获取一个名称/值对,但使用
nodeAttributes.length()
或类似的方法扩展循环应该很容易。

如何在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()

这只是显示了如何获取一个名称/值对,但是使用
nodeAttributes.length()
或类似的东西扩展循环应该很容易。

它返回一个域名NodeMap,我看不出它如何帮助我检索字符串(“name”)和(“ID”),我不需要这些属性的值。我只需要属性名。它返回一个域名NodeMap,我看不出它如何帮助我检索字符串(“name”)和(“ID”),我不需要这些属性的值。我只需要属性名。