-1

For my application I am using a combination of Python+Qt(PySide). I need to read from and write to XML. Currently I have implemented some part of functionality using Python's standard ElementTree module. But it has its drawbacks - for example I cannot do pretty printing of tabbed XML elemens but it rather prints everything in one line, which is machine readable but human undreadable. I cannot use CDATA elements without some hacks... etc.

I tried also Python's xml.dom.minidom module but after I read certain criticism on the web, I am reluctant to continue with it. There are other libraries such as lxml but I do not want to introduce more nonstandard dependencies to my application.

So I was thinking because I am already depending on Qt libraries, whether I should use Qt for all my work with XML. It has QXmlStreamWriter, QXmlStreamReader, QDom* classes etc., all of which I have experience when working with C++/Qt. So, is this a good strategy? Are there any drawbacks? Are Qt XML libraries better (speed, memory, robustness...) than Python's or the opposite is true?

1 Answer 1

3

I do not see the need for switching away just due to a minor feature. I was considering the same question back then, but I decided to use this minor function and the (c)ElementTree will just work for pretty printing:

def indentXmlTree(elem, level = 0): ''' In-place prettyprint formatter for the xml tree ''' i = os.linesep + level * ' ' if len(elem): if not elem.text or not elem.text.strip(): elem.text = i + " " if not elem.tail or not elem.tail.strip(): elem.tail = i for elem in elem: indentXmlTree(elem, level + 1) if not elem.tail or not elem.tail.strip(): elem.tail = i else: if level and (not elem.tail or not elem.tail.strip()): elem.tail = i 

Also note that the most (sophisticated) editors have this feature available for "pretty viewing" even for machine-readable xml files.

Sign up to request clarification or add additional context in comments.

19 Comments

Thank you. I definitely will try this.
@VL.K.: have you resolved the issue?
Not yet, unfortunately. Nevertheless I upvoted your answer as it seems to give a solution to my partial problem. However in my question I was more concerned about whether it makes sense to use XML related stuff from Qt libraries in Python. And how they are compared to Python ET. Pros and cons... ideally from someone who tried both ways. I believe this might be interesting for anybody who is using Python + Qt.
@VL.K.: if that is your real question, I am afraid, it might get closed as too broad and/or opinionated.
Well, I do not think so. What is opinionated about hypothetical answer for example: "I used both and I measured Qt XML handling to be 30 % slower than ET but on the other hand it has this and this and this feature, which ET does not have." I would not call it broad either. If I asked "What do you think of XML?", then yes, it definitely is too broad and opinionated...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.