Seite 1 von 1

[Anfängerfrage] XML Datei parsen

Verfasst: 20. November 2008 00:09
von nierth
Hallo,

nachdem ich langsam am Verzweifeln bin, wende ich mich an dieses Forum. Ziel meines Programms ist es - wie schon aus der Übersicht ersichtlich - ein XML Dokument zu parsen. Mithilfe von Online-Tutorials ahbe ich mir im Verlauf des heutigen Abends folgenden Code zusammengebastelt, welcher leider nicht funktioniert - allem Anschein nach findet er die Datei nicht. Daher meine erste Frage: Wie kan man unter QT sein aktuelles Arbeitsverzeichnis bestimmen bzw. es ändern. Mit QDir?

Code: Alles auswählen

void GuiParser::readData(vector<GuiSensorData> *data) {

	QDomDocument doc("config");
	QFile file("config.xml");

	file.open(stdin, QIODevice::ReadOnly);
	doc.setContent(&file);
	file.close();

	if(file.exists()) {
		cout << "ja";
	}
	else {
		cout << "nein";
	}

	QDomElement root = doc.documentElement();
	QDomNode n1 = root.firstChild();

	while (!n1.isNull()) {
		cout << "Hallo";
		QDomElement e1 = n1.toElement();
		if (!e1.isNull()) {
			double x=0, y=0, beamwidth=0, angle=0, range=0;
			QDomNode n2 = e1.firstChild();
			while (!n2.isNull()) {
				QDomElement e2 = n2.toElement();
				if (!e2.isNull()) {
					if(e2.tagName().startsWith("x-coordinate")) {
						x = e2.text().toDouble();
					} else if(e2.tagName().startsWith("y-coordinate")) {
						y = e2.text().toDouble();
					} else if(e2.tagName().startsWith("beamwidth")) {
						beamwidth = e2.text().toDouble();
					} else if(e2.tagName().startsWith("angle")) {
						angle = e2.text().toDouble();
					} else if(e2.tagName().startsWith("range")) {
						range = e2.text().toDouble();
					}
				}
				n2 = n2.nextSibling();
			}
			GuiSensorData gui(x,y,beamwidth,angle,range);
			data->push_back(gui);
		}
		n1 = n1.nextSibling();
	}
}

anbei noch die xml-Datei

Code: Alles auswählen

<?xml version="1.0"?>
<!DOCTYPE sensorlist SYSTEM "config.dtd">
 
<sensorlist>
		<sensor>
			<title>sensor 1</title>
			<x-coordinate>100</x-coordinate>
			<y-coordinate>200</y-coordinate>
			<beamwidth>120</beamwidth>
			<angle>0</angle>
			<range>60</range>
		</sensor>
		
		<sensor>
			<title>sensor 2</title>
			<x-coordinate>-100</x-coordinate>
			<y-coordinate>200</y-coordinate>
			<beamwidth>120</beamwidth>
			<angle>0</angle>
			<range>60</range>
		</sensor>
		
		<sensor>
			<title>sensor 3</title>
			<x-coordinate>100</x-coordinate>
			<y-coordinate>-200</y-coordinate>
			<beamwidth>120</beamwidth>
			<angle>0</angle>
			<range>60</range>
		</sensor>
		
		<sensor>
			<title>sensor 4</title>
			<x-coordinate>-100</x-coordinate>
			<y-coordinate>-200</y-coordinate>
			<beamwidth>120</beamwidth>
			<angle>0</angle>
			<range>60</range>
		</sensor>
</sensorlist>
Danke schon im Voraus für eure Hilfe,

Thomas

Verfasst: 20. November 2008 08:02
von Christian81
Ich würde QFile einen kompletten Pfad übergeben.
Zur Not kann man auch den Pfad relativ zum Executable mit QCoreApplication::applicationDirPath () rausfinden.

Verfasst: 20. November 2008 17:36
von nierth
mti absoluten Pfad findet er zwar das Dokument, hängt allerdings in der "doc.setContent(&file);" Zeile fest

Verfasst: 20. November 2008 17:50
von Christian81
Dann lies die Datei mal ein und setze den content als QString.