0

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 Answers1

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.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Thank you. I definitely will try this. – HiFile.app - best file manager Jan 10 '14 at 14:45
  • @VL.K.: have you resolved the issue? – László Papp Jan 13 '14 at 17:47
  • 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. – HiFile.app - best file manager Jan 13 '14 at 19:01
  • @VL.K.: if that is your real question, I am afraid, it might get closed as too broad and/or opinionated. – László Papp Jan 13 '14 at 19:02
  • 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... – HiFile.app - best file manager Jan 13 '14 at 19:14
  • @VL.K.: apparently, the people disagreed with you. – László Papp Jan 14 '14 at 13:25
  • People disagree but I never cared for people... that is why I am a programmer not a doctor. Do whatever you like, if you do not like my question delete it. You have the points, you are the boss. :) – HiFile.app - best file manager Jan 14 '14 at 19:35
  • Strange thing happening here. I am with StackOverflow very shortly and never watched my score to closely. Today I started losing points. I do not care too much about it but I just was curious so I looked in history and dicovered I never got a minus, except within last 24 hours I got several of them. Just shortly after I exchanged opinions here with Laszlo. As I say, it is a strange thing... – HiFile.app - best file manager Jan 14 '14 at 19:53
  • @VL.K.: I would suggest to start caring about the site's operation and intended goal if you are willing to contribute its success. :) As for the reputation, you had +1 IIRC, and now you seem to have -1. It means you either got two downvotes or someone retracted the upvote and either that person or someone else gave a downvote. I would not worry much. This type of questions can get a lot more downvotes. – László Papp Jan 14 '14 at 19:55
  • I still do not understand what is unuseful, broad or opinionated about this question. – HiFile.app - best file manager Jan 14 '14 at 20:07
  • @VL.K.: Just let it go then. – László Papp Jan 14 '14 at 20:07
  • Well, then there is a danger that I might ask some more broad and opinionated questions in the future. – HiFile.app - best file manager Jan 14 '14 at 20:14
  • @VL.K.: yes, but you may get automatically locked out by the system if you ask such questions IIRC. – László Papp Jan 14 '14 at 20:15
  • Just to clarify my understanding. The question here is not broad nor opinionated? http://stackoverflow.com/questions/1190206/threading-in-python ... because it seems to me it is. At least compared to my question. Sure it is opinionated! And broad. And nevertheless people think it is useful and give it lots of pluses... – HiFile.app - best file manager Jan 14 '14 at 20:52
  • @VL.K.: that is opinionated, and I just voted for closing it. Anyway, why have you not selected an answer yet? What is missing from my reply? You will not get more answers for closed questions. Since you have 20 questions, and only 3 selected answers altogether, please read this: http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – László Papp Jan 22 '14 at 09:41
  • Why I have not selected an answer? I think it is obvious. Because I got just one answer thank you for early closing of question, I will not get more answers. And it is not actually a complete answer to my question. It does not tell me whether I should better use Qt XML or ET. I just partially solves one issue with ET. It seems that you really want and push your answer to be selected. Sorry, bad luck for you. – HiFile.app - best file manager Jan 22 '14 at 17:17
  • Btw. you should check and forbid many many more questions, Mr. Policeman. For example this, though a very popular one, is clearly opinionated: http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons – HiFile.app - best file manager Jan 22 '14 at 17:22
  • Btw. you suck at math... I have only 8 question, not 20. I reviewed them and marked one answer - mine answer actually to my question. Other questions do not have any answer, so please tell me: What shall I select? And yes, I admit that one question has one answer that might be correct but I have not yet checked if it really works. I owe the author of this answer some resolution... and I will do it soon. Meanwhile please do not force me to select your answers. You are obviously a good programmer, but you are not a person I would like to talk to any more. – HiFile.app - best file manager Jan 22 '14 at 17:34
  • @VL.K.: thank you for your help. That question is closed now as well as per site policy. Please let us know if there are anymore. This is a helpful contribution. – László Papp Jan 23 '14 at 10:20