XML-Datei mit DOM lesen (QT4)
Verfasst: 16. August 2005 18:43
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.
Meine main-methode:
Anstatt der erwarteten Ausgabe bekomme ich:
Was mache ich falsch?
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>
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();
}
}
Code: Alles auswählen
Root-Element - tagName
0x404302
Element-Attributes
0x404612 ; 0x4045a2 ; 0x404542
Element-Attributes
0x404a12 ; 0x4049b2 ; 0x404972