from qgis.core import QgsXmlUtils # Create a new XML document doc = QgsXmlUtils.createXmlDocument() # Create root element and add to document root = QgsXmlUtils.addElement(doc, "root") root.setAttribute("version", "1.0") # Add some child elements to the root QgsXmlUtils.addElement(root, "child1") QgsXmlUtils.addElement(root, "child2") # Convert the XML document to a string xml_str = QgsXmlUtils.documentToString(doc) print(xml_str)
from qgis.core import QgsXmlUtils # Parse an XML string into a document xml_str = "This example parses an XML string into a document and accesses the root element and its attributes. It also prints the child elements of the root using a loop." doc = QgsXmlUtils.stringToDom(xml_str) # Get the root element and its attributes root = doc.documentElement() version = root.attribute("version") # Print the child elements of the root for child in root.childNodes(): print(child.nodeName())