【问题标题】:extract the whole tag data in a QDomElement提取 QDomElement 中的整个标签数据
【发布时间】:2019-09-25 11:25:45
【问题描述】:

我正在使用 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>

我已经可以做的是提取 xywidthheight 等属性标签之间的文本使用以下代码:

    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) 文件来实现它

【问题讨论】:

    标签: xml qt svg tags qtxml


    【解决方案1】:

    好的,我得到了自己的答案,如果其他人遇到此类问题,我也将其留在这里。 您可以使用 QDomNode::operator

    https://doc.qt.io/archives/qt-4.8/qdomnode.html#operator-lt-lt-205

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多