我正在尝试使用 QXmlQuery 从 XML 文件中获取一些元素。一切正常(我能够验证源 XML 文件等),直到我尝试使用 QXmlFormatter 的部分,以便将结果写入另一个 XML 文件。当我到达这部分时,显示以下错误:错误 FODC0002 in tag:trolltech.com,2007:QtXmlPatterns:QIODeviceVariable:inputDocument, at line 1, column 0: Premature end of document。
该代码基于 Qt 中作为示例可用的“Recipes”项目。这里唯一的区别是我制作了一个更简单的“cookbook”XML 文件版本。我尝试使用 QBuffer(示例中实现的方法)而不是文件,但正如预期的那样,得到了相同的结果。
这是源 XML,称为 temp2_xml.xml
<?xml version="1.0" encoding="UTF-8"?>
<cookbook>
<recipe>
<title>Quick and Easy Mushroom Soup</title>
<title>Cheese on Toast</title>
</recipe>
</cookbook>
这是名为 allRecipes.xq 的 Xquery 文件:
(: Select all recipes. :)
declare variable $inputDocument external;
doc($inputDocument)/cookbook/recipe/<p>{string(title)}</p>
这是代码:
QFile aqr_xq("C:/test_xml/allRecipes.xq");
aqr_xq.open(QIODevice::ReadOnly);
QFile file("C:/test_xml/temp_xml.xml");
file.open(QIODevice::ReadWrite);
QFile aqr_r;
aqr_r.setFileName("C:/test_xml/temp2_xml.xml");
aqr_r.open(QIODevice::ReadOnly);
QTextStream in(&aqr_r);
QString inputDocument = in.readAll();
const QString str_query(QString::fromLatin1(aqr_xq.readAll()));
QXmlQuery query;
query.bindVariable("inputDocument",&aqr_r);
query.setQuery(str_query);
bool debug_xml = false;
debug_xml = query.isValid();
QXmlFormatter ser(query, &file);
query.evaluateTo(&ser);
关于导致问题的原因以及如何解决问题的任何想法?