XML-Datei mit DOM lesen (QT4)

Alles rund um die Programmierung mit Qt
Antworten
hulio
Beiträge: 12
Registriert: 12. Mai 2005 12:24

XML-Datei mit DOM lesen (QT4)

Beitrag 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?
Christian81
Beiträge: 7319
Registriert: 26. August 2004 14:11
Wohnort: Bremen
Kontaktdaten:

Beitrag 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;
MfG Christian

'Funktioniert nicht' ist keine Fehlerbeschreibung
hulio
Beiträge: 12
Registriert: 12. Mai 2005 12:24

Beitrag von hulio »

Wenn ich meinen Code so ändere bekomme ich die folgende Ausgabe:

Code: Alles auswählen

Root-Element - tagName


Element-Attributes
 ;  ; 
Element-Attributes
 ;  ; 
BartSimpson
Beiträge: 1379
Registriert: 6. November 2004 12:03
Kontaktdaten:

Beitrag von BartSimpson »

haste mal qDebug versucht?
Antworten