XML Processing - Python

This is a discussion on XML Processing - Python ; 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/&param . Thanks in advance...

+ Reply to Thread
Results 1 to 7 of 7

XML Processing

  1. Default XML Processing

    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/&param.

    Thanks in advance


  2. Default Re: XML Processing

    On Aug 2, 1:45 pm, Roman <rgelfa...@hotmail.com> 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/pyt...er/016814.html

    I would think that you could use regular expressions too.

    Mike


  3. Default Re: XML Processing

    Robert Dailey wrote:
    > Both strings in your example are exactly the same, unless I'm missing
    > something.
    >
    > On 8/2/07, Roman <rgelfand2@hotmail.com> 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

  4. Default Re: XML Processing

    kyosohma wrote:
    > On Aug 2, 1:45 pm, Roman <rgelfa...@hotmail.com> 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/pyt...er/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


  5. Default Re: XML Processing

    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

  6. Default Re: XML Processing

    On Aug 2, 2:09 pm, Jay Loden <pyt...@jayloden.com> wrote:
    > kyoso... wrote:
    > > On Aug 2, 1:45 pm, Roman <rgelfa...@hotmail.com> 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/pyt...er/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.


  7. Default Re: XML Processing

    In article <1186082619.193097.207650@x40g2000prg.googlegroups.com>,
    <kyosohma> wrote:
    >On Aug 2, 2:09 pm, Jay Loden <pyt...@jayloden.com> wrote:
    >> kyoso... wrote:
    >> > On Aug 2, 1:45 pm, Roman <rgelfa...@hotmail.com> 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/pyt...er/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.

+ Reply to Thread

Similar Threads

  1. In PDF Processing...
    By Application Development in forum Java
    Replies: 4
    Last Post: 11-21-2007, 08:41 AM
  2. Re: In PDF Processing...
    By Application Development in forum Java
    Replies: 0
    Last Post: 11-15-2007, 09:34 AM
  3. PDF Processing
    By Application Development in forum Java
    Replies: 0
    Last Post: 11-15-2007, 08:16 AM
  4. Help with XML processing using DOM
    By Application Development in forum Java
    Replies: 9
    Last Post: 06-30-2007, 07:49 AM
  5. Processing
    By Application Development in forum Inetserver
    Replies: 1
    Last Post: 05-15-2007, 02:10 PM