XML Processing

Discussion in 'Python' started by Roman, Aug 2, 2007.

  1. Roman

    Roman Guest

    Roman, Aug 2, 2007
    #1
    1. Advertising

  2. Roman

    Guest

    On Aug 2, 1:45 pm, Roman <> wrote:
    > Is there a package that converts a string that contains special
    > characters in xml to to literal value. For instance, converts stringhttp://myhome/&paramtohttp://myhome/&param.
    >
    > Thanks in advance


    I've seen examples using the HTMLgen module. But here's another script
    I just found:

    http://mail.python.org/pipermail/python-list/1999-November/016814.html

    I would think that you could use regular expressions too.

    Mike
     
    , Aug 2, 2007
    #2
    1. Advertising

  3. Roman

    Jay Loden Guest

    Robert Dailey wrote:
    > Both strings in your example are exactly the same, unless I'm missing
    > something.
    >
    > On 8/2/07, Roman <> wrote:
    >> Is there a package that converts a string that contains special
    >> characters in xml to to literal value. For instance, converts string
    >> http://myhome/&param to http://myhome/&amp;param.
    >>
    >> Thanks in advance
    >>
    >> --
    >> http://mail.python.org/mailman/listinfo/python-list



    Robert, your newsreader/mail program is converting the HTML/XML entity codes into the corresponding character so you're not seeing the plain text version of what the OP posted. He's asking for code to convert XML entities like '&' into their escaped strings.

    I believe xml.sax.saxutils.encode() is what the OP is looking for:

    >>> import xml.sax.saxutils
    >>> xml.sax.saxutils.escape('http://myhome/&param')

    'http://myhome/&amp;param'

    -Jay
     
    Jay Loden, Aug 2, 2007
    #3
  4. Roman

    Jay Loden Guest

    wrote:
    > On Aug 2, 1:45 pm, Roman <> wrote:
    >> Is there a package that converts a string that contains special
    >> characters in xml to to literal value. For instance, converts stringhttp://myhome/&paramtohttp://myhome/&param.
    >>
    >> Thanks in advance

    >
    > I've seen examples using the HTMLgen module. But here's another script
    > I just found:
    >
    > http://mail.python.org/pipermail/python-list/1999-November/016814.html
    >
    > I would think that you could use regular expressions too.
    >
    > Mike


    I would reccommend against using the example above or using regular expressions, since both are likely to miss corner cases, and the stdlib has an encode function that's already designed for this task

    http://docs.python.org/lib/module-xml.sax.saxutils.html

    The encode() function in xml.sax.saxutils also is extensible, should you run into any entities that it fails to escape in the current implementation.

    -Jay
     
    Jay Loden, Aug 2, 2007
    #4
  5. Roman schrieb:
    > Is there a package that converts a string that contains special
    > characters in xml to to literal value. For instance, converts string
    > http://myhome/&param to http://myhome/&amp;param.


    import xml.sax.saxutils

    print xml.sax.saxutils.escape("I'm a happy & friendly guy, and 1 < 3 -
    never forget that!")

    Diez
     
    Diez B. Roggisch, Aug 2, 2007
    #5
  6. Roman

    Guest

    On Aug 2, 2:09 pm, Jay Loden <> wrote:
    > wrote:
    > > On Aug 2, 1:45 pm, Roman <> wrote:
    > >> Is there a package that converts a string that contains special
    > >> characters in xml to to literal value. For instance, converts stringhttp://myhome/&paramtohttp://myhome/&param.

    >
    > >> Thanks in advance

    >
    > > I've seen examples using the HTMLgen module. But here's another script
    > > I just found:

    >
    > >http://mail.python.org/pipermail/python-list/1999-November/016814.html

    >
    > > I would think that you could use regular expressions too.

    >
    > > Mike

    >
    > I would reccommend against using the example above or using regular expressions, since both are likely to miss corner cases, and the stdlib has an encode function that's already designed for this task
    >
    > http://docs.python.org/lib/module-xml.sax.saxutils.html
    >
    > The encode() function in xml.sax.saxutils also is extensible, should you run into any entities that it fails to escape in the current implementation.
    >
    > -Jay


    Yes, I am an idiot.
     
    , Aug 2, 2007
    #6
  7. In article <>,
    <> wrote:
    >On Aug 2, 2:09 pm, Jay Loden <> wrote:
    >> wrote:
    >> > On Aug 2, 1:45 pm, Roman <> wrote:
    >> >> Is there a package that converts a string that contains special
    >> >> characters in xml to to literal value. For instance, converts

    >stringhttp://myhome/&paramtohttp://myhome/&param.
    >>
    >> >> Thanks in advance

    >>
    >> > I've seen examples using the HTMLgen module. But here's another script
    >> > I just found:

    >>
    >> >http://mail.python.org/pipermail/python-list/1999-November/016814.html

    >>
    >> > I would think that you could use regular expressions too.

    >>
    >> > Mike

    >>
    >> I would reccommend against using the example above or using regular

    >expressions, since both are likely to miss corner cases, and the stdlib
    >has an encode function that's already designed for this task
    >>
    >> http://docs.python.org/lib/module-xml.sax.saxutils.html
    >>
    >> The encode() function in xml.sax.saxutils also is extensible, should

    >you run into any entities that it fails to escape in the current
    >implementation.
    >>
    >> -Jay

    >
    >Yes, I am an idiot.
    >


    ! Unproved, as Scotsmen are wont to say.

    This and similar questions arise frequently enough that I recommend
    <URL: http://wiki.python.org/moin/EscapingXml > for those who want
    to pursue the subject.
     
    Cameron Laird, Aug 3, 2007
    #7
    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. Bomb Diggy
    Replies:
    0
    Views:
    452
    Bomb Diggy
    Jul 28, 2004
  2. Oleg  Paraschenko
    Replies:
    0
    Views:
    382
    Oleg Paraschenko
    Jun 6, 2005
  3. knorth
    Replies:
    0
    Views:
    368
    knorth
    Nov 5, 2005
  4. Hubert Hung-Hsien Chang
    Replies:
    2
    Views:
    517
    Michael Foord
    Sep 17, 2004
  5. Tomasz Wegrzanowski

    magic/xml library for easy XML processing

    Tomasz Wegrzanowski, Aug 5, 2006, in forum: Ruby
    Replies:
    5
    Views:
    95
    Tomasz Wegrzanowski
    Aug 6, 2006
Loading...

Share This Page