Re: Remove whitespaces and line breaks in a XML file

Discussion in 'Python' started by Stefan Behnel, Feb 7, 2011.

  1. David Vicente, 07.02.2011 18:45:
    > I´m parsing an xml file with xml.etree. It works correctly, but I have a
    > problem with the text attribute of the elements which should be empty. For
    > example, in this case:
    >
    > <book>
    >
    > <author>Ken<author>
    >
    > </book>
    >
    >
    >
    > The text element of “book†should be empty, but it returns me some
    > whitespaces and break lines. I can´t get remove these whitespaces without
    > remove information.


    Only a DTD (or schema) can provide the information which whitespace in an
    XML document is meaningful and which isn't, so there is no generic way to
    "do it right", especially not for something as generic as an XML parser.

    What may work for you is to check if an Element has children and only
    whitespace as text ("not el.text or not el.text.strip()"), and only then
    replace it by None.

    Stefan
     
    Stefan Behnel, Feb 7, 2011
    #1
    1. Advertising

  2. I´ll try to mix it with my code (xml.etree).
    Thanks ;)


    -----Mensaje original-----
    De: python-list-bounces+dvicente=
    [mailto:python-list-bounces+dvicente=] En nombre
    de Josh English
    Enviado el: martes, 08 de febrero de 2011 0:46
    Para:
    CC:
    Asunto: Re: Remove whitespaces and line breaks in a XML file

    I found the code posted at

    http://infix.se/2007/02/06/gentlemen-indent-your-xml

    quite helpful in turning my xml into human-readable structures. It works
    best for XML-Data.

    Josh
    --
    http://mail.python.org/mailman/listinfo/python-list
     
    David Vicente, Feb 9, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joona I Palaste
    Replies:
    1
    Views:
    494
    Berco
    Jan 7, 2004
  2. Berco
    Replies:
    0
    Views:
    1,711
    Berco
    Jan 7, 2004
  3. kaushikshome
    Replies:
    4
    Views:
    795
    kaushikshome
    Sep 10, 2006
  4. jochen kaechelin

    remove all whitespaces in a string

    jochen kaechelin, May 28, 2007, in forum: Ruby
    Replies:
    8
    Views:
    129
    Florian Aßmann
    May 31, 2007
  5. Marek Stepanek
    Replies:
    12
    Views:
    422
    Peter J. Holzer
    Sep 2, 2006
Loading...

Share This Page