将 QDomElement 转换为 QString / Container 类
假设我们有以下 XML 文档:
<root>
<options>
...
</options>
<children>
<child name="first">12345</child>
<child name="second">
<additionalInfo>abcd</additionalInfo>
</children>
</root>
我想获取“子”节点的字符串表示形式并将它们附加到数组中(我不想丢失 XML 语法,因此 .text() 不是一个选项) 。例如,第一个孩子看起来像:
QString child = "<child name="first">12345</child>";
我使用以下代码来获取元素:
QDomDocument doc;
QDomElement element;
element = xml->documentElement();
if(element.isNull() == false)
{
element = element.firstChildElement("children");
if(element.isNull()) return;
element = element.firstChildElement("child");
while(element.isNull() == false)
{
doc = element.toDocument();
if(doc.isNull() == false)
{
// save string into array
array.append(doc.toString());
}
element = element.nextSiblingElement("child");
}
}
问题是 doc.isNull 返回始终 false (看起来我无法将元素转换为文档)。有什么方法可以执行此操作吗?
编辑:
我想补充一点,QString 在这里不是强制性的。基本上任何稍后可用于检索数据的类都可以(我将保存这些节点并稍后使用它们来初始化另一个对象)。重要的是,即使原始文档已被破坏,我也应该能够访问这些值。例如,可以将这些元素直接存储到某个数组(例如 QList),稍后可以使用该数组来访问它们。
Let's say we have the following XML document:
<root>
<options>
...
</options>
<children>
<child name="first">12345</child>
<child name="second">
<additionalInfo>abcd</additionalInfo>
</children>
</root>
I would like to get a string representation of the "child" nodes and append them into an array (I don't want to lose the XML syntax so .text() is not an option). For example, the first child would look like:
QString child = "<child name="first">12345</child>";
I used the following code to get the elements:
QDomDocument doc;
QDomElement element;
element = xml->documentElement();
if(element.isNull() == false)
{
element = element.firstChildElement("children");
if(element.isNull()) return;
element = element.firstChildElement("child");
while(element.isNull() == false)
{
doc = element.toDocument();
if(doc.isNull() == false)
{
// save string into array
array.append(doc.toString());
}
element = element.nextSiblingElement("child");
}
}
The problem is that the doc.isNull returns always false (looks like I'm unable to convert the element into document). Is there any way how I can perform this?
Edit:
I would like to add that QString is not mandatory here. Basically any class that can be later used to retrieve the data is ok (I'll save these nodes and use them to initialize another objects later on). Important thing is that I should be able to access those values even when the original document have been destroyed.For example, it it possible to store those elements directly to some array (e.g. QList), which can be used to access them later on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我将添加我自己问题的答案。不知道为什么,但看起来我错过了文档中的以下功能。
void QDomNode::save ( QTextStream & str, int indent ) const
它几乎完成了我将节点转换为字符串所需的所有操作,例如:
I'll add an answer to my own question. No idea why, but looks like I missed the following function in the documentation.
void QDomNode::save ( QTextStream & str, int indent ) const
It does pretty much all I need to convert a node into a string, e.g:
由于您需要 XML 格式本身,因此不需要
QDomElement
或QDomDocument
。QDomElement
和QDomDocument
用于获取存储在 XML 文档中的数据。你只需要一个普通的文件遍历。
使用打开文件
您可以读取文件的全部内容,
您可以将其分配给
QString
。例如,
基于 newline
\n
字符拆分整个内容然后您可以使用Now 每个索引对应于 XML 文件中的每一行, 。您可以遍历它并获得所需的兴趣线。
希望它有帮助...
Since you need the XML format itself you don't need
QDomElement
orQDomDocument
.QDomElement
andQDomDocument
are used to obtain the data stored in the XML documents.You just need a ordinary file traversal.
Open the file using
You can read the entire contents of the file by,
which you can assign it to an
QString
.For e.g.,
Then you can split the entire contents based on the newline
\n
character usingNow each index corresponds to each line in the XML file. You can traverse through it and obtain the desired lines of interest.
Hope it helps...
好吧,我认为你不能完全用 Qt XML 类做你想要的事情,但应该可以根据 Qt 的方法自己简单地重建字符串(也许不 100% 匹配原始字符串,但具有相同的含义) XML 类提供。
编辑:
可能会做这件事的小代码片段(未经测试):
Well i think you cant do excactly what you want with the Qt XML classes, but it should be possible to simply reconstruct the string yourself (perhaps not matching the original in 100%, but with the same meaning), based on the methods the Qt XML classes provide.
EDIT:
Small code snippet which might do the thing (untested):
感谢您分享您的问题并发布答案。
我找到了另一个解决方案,将有趣的元素复制到一个新文档中,然后将其序列化。
现在
result
包含序列化的child
元素。Thank you for sharing your question and posting an answer.
I found another solution by copying the interesting elements into a fresh document, that is then serialized.
Now
result
contains the serializedchild
element.