extract the whole tag data in a QDomElement

299 Views Asked by At

I'm using QtXml to manipulate some svg file, what I'm trying to do is that when I find a specific QDomElement in the document I want to try to save the whole xml element data (with all of it's children) into a QString not just their attributes and or texts between tags.

imagine this tag: (the g tag)

<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>

what I already can do is to extract attributes like x, y , width, height and also texts between tags using codes below:

    int x = gzElement.attribute("x").toInt(); // x=25
    int y = gzElement.attribute("y").toInt(); // y=50
    QString sampleText = gzElement.text(); // sampleText = Screen

but what I'm not able to do and trying to reach them is to get all of the g tag and it's child elements like this:

//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>" */

does anybody knows an API function in Qt library or other alternative methods? or I have to implement it using pure QString and QRegExp classes to traverse svg (xml) file manually

1

There are 1 best solutions below

0
Morteza Sherafati On

ok I got my own answer and I leave it here also in the case that someone else encounter this type of problem. you can use QDomNode::operator<< to save all of the dom data tags with all of it's children to a QTextStream using this function described in Qt Doc.

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