Seite 1 von 1

XML-Datei mit DOM lesen (QT4)

Verfasst: 16. August 2005 18:43
von hulio
Hallo zusammen,

ich haben ein Problem beim Lesen einer XML-Datei mit Hilfe von QDomDocument.

Ich wollte einige Elemente der folgenden XML-Datei auf die Konsole auszugeben.

Code: Alles auswählen

<!DOCTYPE AdBookML>
<adbook>
 <contact email="kal@goteborg.se" phone="+46(0)31 123 4567" name="Kal" />
 <contact email="ada@goteborg.se" phone="+46(0)31 765 1234" name="Ada" />
</adbook>
Meine main-methode:

Code: Alles auswählen

int main()
{
    QDomDocument doc( "AdBookML" );
    QFile file( "/tmp/test.xml" );
    if( !file.open( QIODevice::ReadOnly ) )
        return -1;
    if( !doc.setContent( &file ) )
    {
        file.close();
        return -2;
    }
    file.close();
    
    QDomElement root = doc.documentElement();
    if( root.tagName() != "adbook" )
        return -3;
    
    std::cout << "Root-Element - tagName" << std::endl;
    std::cout << root.tagName().constData() << std::endl << std::endl;
    
    QDomNode n = root.firstChild();
    while( !n.isNull() )
    {
        QDomElement e = n.toElement();
        if( !e.isNull() )
        {
            if( e.tagName() == "contact" )
            {
                std::cout << "Element-Attributes" << std::endl;
                std::cout << e.attribute( "name", "" ).constData() << " ; ";
                std::cout << e.attribute( "phone", "" ).constData() << " ; ";
                std::cout << e.attribute( "email", "" ).constData() << std::endl;
            }
        }
        
        n = n.nextSibling();
    }
}
Anstatt der erwarteten Ausgabe bekomme ich:

Code: Alles auswählen

Root-Element - tagName
0x404302

Element-Attributes
0x404612 ; 0x4045a2 ; 0x404542
Element-Attributes
0x404a12 ; 0x4049b2 ; 0x404972
Was mache ich falsch?

Verfasst: 16. August 2005 18:47
von Christian81
cout interpretiert .constData() nicht als const char* und gibt es deshalb als Pointer aus.
probier mal std::cout << (const char*)(root.tagName().constData()) << std::endl << std::endl;

Verfasst: 16. August 2005 18:54
von hulio
Wenn ich meinen Code so ändere bekomme ich die folgende Ausgabe:

Code: Alles auswählen

Root-Element - tagName


Element-Attributes
 ;  ; 
Element-Attributes
 ;  ; 

Verfasst: 16. August 2005 19:04
von BartSimpson
haste mal qDebug versucht?