qt - Changing the root element of an xml document (svg) -
i need work svg objects using qgraphicssvgitem
sublcass.
i learning svg - , have noticed is, svg shows fine if root element <svg ..>
yet, playing w3schools samples, noticed examples embedded in html, , possible code have process both kinds (simple svg, html containing svg).
so, solution see is, extract svg element (with kids) , replace root element it.
in mind code clear:
qdomdocument _svgxml; void setcontentfromfile(qstring filename) { qfile file(filename); file.open(qfile::readonly | qfile::text); qtextstream in(&file); qstring data = in.readall(); file.close(); _svgxml.setcontent(svgcontent); checkroot(); } void checkroot() { qdomelement rootelem = _svgxml.documentelement(); recursivelycheckroot(rootelem); qdebug(qprintable(qstring("root ") + rootelem.nodename())); // or qdebug(_svgxml.tobytearray()); } void recursivelycheckroot(qdomelement& rootelem) { if(rootelem.nodename() == "svg") return; qdomnode n = rootelem.firstchild(); while(!n.isnull()) { if(n.iselement()) { qdomelement e = n.toelement(); if(e.nodename() == "svg") { rootelem = e; return; } recursivelycheckroot(e); } n = n.nextsibling(); } }
the problem doesn't work. see no change.
please me extract svg element , make root... discarding else.
sample source:
<!doctype html> <html> <body> <svg width="400" height="150"> <defs> <lineargradient id="grad1" y1="0%" x1="0%" y2="0%" x2="100%"> <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1"/> <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1"/> </lineargradient> </defs> <ellipse fill="url(#grad1)" cx="200" cy="70" rx="85" ry="55"/> <text x="150" y="86" fill="green" font-family="verdana" font-size="45">svg</text> sorry, browser not support inline svg. </svg> </body> </html>
desired result:
<svg width="400" height="150"> <defs> <lineargradient id="grad1" y1="0%" x1="0%" y2="0%" x2="100%"> <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1"/> <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1"/> </lineargradient> </defs> <ellipse fill="url(#grad1)" cx="200" cy="70" rx="85" ry="55"/> <text x="150" y="86" fill="green" font-family="verdana" font-size="45">svg</text> sorry, browser not support inline svg. </svg>
(the !doctype
or other declarations stay)
once understood - based on robert longson's comment "once html document, html document" ... realized cannot modify existing document, had make new one.
void recursivelycheckroot(qdomelement rootelem) { if(rootelem.nodename() == "svg") return; qdomnode n = rootelem.firstchild(); while(!n.isnull()) { if(n.iselement()) { qdomelement e = n.toelement(); if(e.nodename() == "svg") { qstring str; qtextstream stream(&str); e.save(stream, qdomnode::documentnode); _svgxml.setcontent(str); return; } recursivelycheckroot(e); } n = n.nextsibling(); } }
Comments
Post a Comment