Ich habe mit der QT Klasse QDomDocument versucht einen parser für XML zu schreiben allerdings komm ich nicht so ganz vorran. Ich komme einfach nicht über den Punkt hinaus die unterelemente von den Tags <message> zu wechseln.
ein Beispiel:
Code: Alles auswählen
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="en_US" sourcelanguage="en_GB">
<context>
<name>client</name>
<message>
<source>Refresh</source>
<comment>action name</comment>
<translation>Refresh</translation>
</message>
<message>
<source>All Measures</source>
<comment>action name</comment>
<translation>All Measures</translation>
</message>
<message>
<source>normal</source>
<translation>normal</translation>
</message>
</name>
</context>
</TS>
und hier nochmal mein Code fals er benötigt wird.
Hier ist mein Aufruf.
Code: Alles auswählen
QFile xmlFile(targetPath);
xmlFile.open(QIODevice::ReadOnly);
QByteArray xmlData(xmlFile.readAll());
myDoc.setContent(xmlData);
QDomElement root = doc.documentElement();
QDomNode node = root.firstChild();
while(!node.isNull())
{
if(node.toElement().tagName() == "context")
{
parseEntry(node.toElement());
node = node.nextSibling();
}
}
Parseklasse.
Code: Alles auswählen
void MainWindow::parseEntry(const QDomElement &element)
{
QDomNode node = element.firstChild();
while(!node.isNull())
{
if(node.toElement().tagName() == "name"){
QDomNode childNode = node.firstChild();
while(!childNode.isNull())
{
if(childNode.nodeType() == QDomNode::TextNode)
{
QDomNode underChild = childNode.firstChild();
QString stringChild = underChild.toElement().tagName();
}
childNode = childNode.nextSibling();
}
}else if(node.toElement().tagName() == "message")
{
QDomNode childNode = node.firstChild();
while(!childNode.isNull())
{
if(childNode.nodeType() == QDomNode::TextNode)
{
QDomNode underChild = childNode.firstChild();
QString stringChild = underChild.toElement().tagName();
}
childNode = childNode.nextSibling();
}
}
node = node.nextSibling();
}
}
lg Necropsy