我试图弄清楚QXmlStreamReader是如何为我正在编写的C++应用程序工作的。我想解析的XML文件是一个大型字典,具有复杂的结构和大量的Unicode字符,所以我决定用一个更简单的文档尝试一个小的测试用例。不幸的是,我撞到了墙。下面是xml文件示例:
<?xml version="1.0" encoding="UTF-8" ?>
<persons>
<person>
<firstname>John</firstname>
<surname>Doe</surname>
<email>john.doe@example.com</email>
<website>http://en.wikipedia.org/wiki/John_Doe</website>
</person>
<person>
<firstname>Jane</firstname>
<surname>Doe</surname>
<email>jane.doe@example.com</email>
<website>http://en.wikipedia.org/wiki/John_Doe</website>
</person>
<person>
<firstname>Matti</firstname>
<surname>Meikäläinen</surname>
<email>matti.meikalainen@example.com</email>
<website>http://fi.wikipedia.org/wiki/Matti_Meikäläinen</website>
</person>
</persons>...and --我试图使用以下代码来解析它:
int main(int argc, char *argv[])
{
if (argc != 2) return 1;
QString filename(argv[1]);
QTextStream cout(stdout);
cout << "Starting... filename: " << filename << endl;
QFile file(filename);
bool open = file.open(QIODevice::ReadOnly | QIODevice::Text);
if (!open)
{
cout << "Couldn't open file" << endl;
return 1;
}
else
{
cout << "File opened OK" << endl;
}
QXmlStreamReader xml(&file);
cout << "Encoding: " << xml.documentEncoding().toString() << endl;
while (!xml.atEnd() && !xml.hasError())
{
xml.readNext();
if (xml.isStartElement())
{
cout << "element name: '" << xml.name().toString() << "'"
<< ", text: '" << xml.text().toString() << "'" << endl;
}
else if (xml.hasError())
{
cout << "XML error: " << xml.errorString() << endl;
}
else if (xml.atEnd())
{
cout << "Reached end, done" << endl;
}
}
return 0;
}...then --我得到了这个输出:
C:\xmltest\Debug>xmltest.exe example.xml
开始..。文件名: example.xml
打开的文件可以
编码:
XML错误:遇到错误编码的内容。
发生了什么?这个文件再简单不过了,在我看来是一致的。对于我的原始文件,我还获得了编码的空白条目,条目的名称()被显示,但是遗憾的是,text()也是空的。任何建议都非常感谢,就我个人而言,我完全迷惑了。
发布于 2013-04-03 12:32:06
试试看这个例子,我刚刚从我的项目中复制了它,它对我有用。
void MainWindow::readXML(const QString &fileName)
{
fileName = "D:/read.xml";
QFile* file = new QFile(fileName);
if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::critical(this, "QXSRExample::ReadXMLFile", "Couldn't open xml file", QMessageBox::Ok);
return;
}
/* QXmlStreamReader takes any QIODevice. */
QXmlStreamReader xml(file);
/* We'll parse the XML until we reach end of it.*/
while(!xml.atEnd() && !xml.hasError())
{
/* Read next element.*/
QXmlStreamReader::TokenType token = xml.readNext();
/* If token is just StartDocument, we'll go to next.*/
if(token == QXmlStreamReader::StartDocument)
continue;
/* If token is StartElement, we'll see if we can read it.*/
if(token == QXmlStreamReader::StartElement) {
if(xml.name() == "email") {
ui->listWidget->addItem("Element: "+xml.name().toString());
continue;
}
}
}
/* Error handling. */
if(xml.hasError())
QMessageBox::critical(this, "QXSRExample::parseXML", xml.errorString(), QMessageBox::Ok);
//resets its internal state to the initial state.
xml.clear();
}
void MainWindow::writeXML(const QString &fileName)
{
fileName = "D:/write.xml";
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox::critical(this, "QXSRExample::WriteXMLFile", "Couldn't open anna.xml", QMessageBox::Ok);
return;
}
QXmlStreamWriter xmlWriter(&file);
xmlWriter.setAutoFormatting(true);
xmlWriter.writeStartDocument();
//add Elements
xmlWriter.writeStartElement("bookindex");
ui->listWidget->addItem("bookindex");
xmlWriter.writeStartElement("Suleman");
ui->listWidget->addItem("Suleman");
//write all elements in xml filexl
xmlWriter.writeEndDocument();
file.close();
if (file.error())
QMessageBox::critical(this, "QXSRExample::parseXML", file.errorString(), QMessageBox::Ok);
}https://stackoverflow.com/questions/4201175
复制相似问题