我正在使用 QtXml 来操作一些 svg 文件,我想要做的是,当我在文档中找到特定的 QDomElement 时,我想尝试将整个 xml 元素数据(及其所有子元素)保存到 QString不仅仅是它们的属性和/或标签之间的文本。
想象一下这个标签:(g标签)
<g fill="#000000" fill-opacity="0" stroke="#000000" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,0,0)"
font-family="MS Shell Dlg 2" font-size="8.25" font-weight="400" font-style="normal"
>
<rect x="25" y="50" width="50" height="25"/>
<text x="50" y="26" font-size="24" stroke-width="2px" text-anchor="middle">Screen</text>
</g>
我已经可以做的是使用以下代码提取诸如x、y、width、height等属性以及标签之间的文本:
int x = gzElement.attribute("x").toInt(); // x=25
int y = gzElement.attribute("y").toInt(); // y=50
QString sampleText = gzElement.text(); // sampleText = Screen
但我无法做到并试图联系他们的是获取所有g标签,它是这样的子元素:
//dummy code
QString tagData = gzElement.wholeData(); /* tagData =
"<g fill="#000000" fill-opacity="0" stroke="#000000" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,0,0)"
font-family="MS Shell Dlg 2" font-size="8.25" font-weight="400" font-style="normal"
>
<rect x="25" y="50" width="50" height="25"/>
<text x="50" y="26" font-size="24" stroke-width="2px" text-anchor="middle">Screen</text>
</g>" */
有人知道 Qt 库中的 API 函数或其他替代方法吗?或者我必须使用纯 QString 和 QRegExp 类手动遍历 svg (xml) 文件来实现它