I need to change some element attributes of a xml file. I've been trying to achieve this using QXmlStreamWriter class, but it does not work fine, because when I call the function, all elements are deleted of the xml file, except the changed attribute.
For example, I want to change the width of a rectangle, in a SVG file, keeping the other elements and attributes intact.
Here is my code:
void Editor
::changeElementAttribute(int pos,
QVariant newData
) {
DomItem *item = static_cast<DomItem*>(treeView->currentIndex().internalPointer());
file.setFileName(currentPath);
{
tr("Cannot write file %1:\n%2.").arg(currentPath)
.arg(file.errorString()));
return;
}
stream.setDevice(&file);
stream.writeStartElement(node.nodeName()); //tagname
stream.writeAttribute(attributeMap.item(pos).nodeName(), newData.toString()); //attribute and value
stream.writeEndElement();
}
void Editor::changeElementAttribute(int pos, QVariant newData)
{
DomItem *item = static_cast<DomItem*>(treeView->currentIndex().internalPointer());
QDomNode node = item->node();
QDomNamedNodeMap attributeMap = node.attributes();
file.setFileName(currentPath);
if (!file.open(QFile::WriteOnly | QFile::Text))
{
QMessageBox::warning(new QWidget(), tr("SVG Editor"),
tr("Cannot write file %1:\n%2.").arg(currentPath)
.arg(file.errorString()));
return;
}
stream.setDevice(&file);
stream.writeStartElement(node.nodeName()); //tagname
stream.writeAttribute(attributeMap.item(pos).nodeName(), newData.toString()); //attribute and value
stream.writeEndElement();
}
To copy to clipboard, switch view to plain text mode
Any help is welcome.
Thanks in advanced!
Bookmarks