QDomDocument doc; QDomElement root = doc.createElement("root"); doc.appendChild(root); QDomAttr attr = doc.createAttribute("key"); attr.setValue("value"); root.setAttributeNode(attr);
QDomDocument doc; QDomElement root = doc.createElement("root"); doc.appendChild(root); QDomAttr attr = doc.createAttribute("description"); attr.setValue("This is a \"quoted\" string."); root.setAttributeNode(attr);This code creates a new QDomDocument, and appends an element node "root". Then, it creates an attribute node "description" with a value that contains double quotes. The quotes are escaped with backslashes, so that the resulting XML will be valid. In both examples, the createAttribute() function creates a new attribute node, and sets its name and value. The attribute is then added to the parent element with the setAttributeNode() function. The QDomDocument class provides many more functions for working with XML documents and elements.