Saving and Loading XML Files

Both PyQt and the Python standard library can read and write XML files. PyQt provides two parsers for reading, and can write XML using its QDomDocu-ment class. PyQt 4.3 adds two new XML classes. The QXmlStreamReader class is lightweight like SAX, but easier to use, and the QXmlStreamWriter class is much easier and more efficient for writing than writing by hand or using DOM. The Python standard library also provides extensive XML support, but in this section we will confine ourselves to the functionality offered by the PyQt library, since Python's XML classes are well covered by Python's documentation and in such books as Python and XML and XML Processing in Python.

XML formats tend to be a lot more verbose than plain text formats, and not so easy for humans to read. On the other hand, encoding issues are taken care of, so hand editing can be more reliable than with plain text, and the parsing of the overall file structure is usually a lot easier using a suitable XML library than for plain text files. XML formats are generally simpler to extend than either binary or plain text formats, although care must be taken when writing XML to ensure that data does not contain XML meta-characters. Writing XML is straightforward, but reading it requires the use of a parser. There are two very different and widely used XML parser APIs: DOM (Document Object Model), which loads entire XML documents into memory and is well suited to editing a document's structure, and SAX (Simple API for XML), which works incrementally, so is less resource-hungry and is suitable for searching and processing XML documents. We will show both parsers in action.

Was this article helpful?

0 -3
SEO Made Easy Mind Map

SEO Made Easy Mind Map

This is a quick start guide to skyrocketing your offline and online business with search engines.

Get My Free Ebook


Post a comment