Create xml with elementtree ET and xml escaping

Discussion in 'Python' started by nenad.cikic@gmail.com, Dec 11, 2012.

  1. Guest

    Hello, I have posted the same in XML group but it seems pretty dead there so I will repost here.

    I am new to xml processing in python.
    I am looking to create XML. Xml is not too difficult so I thought to createit manually using ElementTree.
    First I noted that ET.toString does escape <>& but not " and '
    Is that normal?

    Since I have also the need to sign the XML I need the ability to create xmlbut without xml escaping (unescaped data are signed).
    If i do ET.toString(root,'utf8',text') i do not get the xml tags and if I do ET.toString(root,'utf8') I get escaped chars.
    For example:
    a=ET.Element('a')
    b=ET.SubElement(a,'b')
    b.text=u"Å¡Ä‘<>&"

    ET.tostring(a,'utf8')
    outputs to
    "<?xml version='1.0' encoding='utf8'?>\n<a><b>\xc5\xa1\xc4\x91&lt;&gt;&amp;</b></a>"

    ET.tostring(a,'utf8',method='text')
    outputs to
    "\xc5\xa1\xc4\x91<>&"

    and I need before singing
    <a><b>\xc5\xa1\xc4\x91<>&</b></a>
    and after signing
    <a><b>\xc5\xa1\xc4\x91&lt;&gt;&amp;</b></a>

    Is there some way other than string replace?
    Thanks
    Nenad
    , Dec 11, 2012
    #1
    1. Advertising

  2. MRAB Guest

    On 2012-12-11 17:47, wrote:
    > Hello, I have posted the same in XML group but it seems pretty dead there so I will repost here.
    >
    > I am new to xml processing in python.
    > I am looking to create XML. Xml is not too difficult so I thought to create it manually using ElementTree.
    > First I noted that ET.toString does escape <>& but not " and '
    > Is that normal?
    >

    " needs to be encoded when it's in an attribute's value:

    <tag value="a quote (&quot;)">

    because it's also being used as a delimiter in that case, but elsewhere
    it has no special meaning.

    > Since I have also the need to sign the XML I need the ability to create xml but without xml escaping (unescaped data are signed).


    XML with the escaping isn't valid XML.

    > If i do ET.toString(root,'utf8',text') i do not get the xml tags and if I do ET.toString(root,'utf8') I get escaped chars.
    > For example:
    > a=ET.Element('a')
    > b=ET.SubElement(a,'b')
    > b.text=u"Å¡Ä‘<>&"
    >
    > ET.tostring(a,'utf8')
    > outputs to
    > "<?xml version='1.0' encoding='utf8'?>\n<a><b>\xc5\xa1\xc4\x91&lt;&gt;&amp;</b></a>"
    >
    > ET.tostring(a,'utf8',method='text')
    > outputs to
    > "\xc5\xa1\xc4\x91<>&"
    >
    > and I need before singing
    > <a><b>\xc5\xa1\xc4\x91<>&</b></a>
    > and after signing
    > <a><b>\xc5\xa1\xc4\x91&lt;&gt;&amp;</b></a>
    >
    > Is there some way other than string replace?
    >
    MRAB, Dec 11, 2012
    #2
    1. Advertising

  3. Guest

    Il giorno martedì 11 dicembre 2012 20:59:54 UTC+1, MRAB ha scritto:
    > > Hello, I have posted the same in XML group but it seems pretty dead there so I will repost here.

    >
    > >

    >
    > > I am new to xml processing in python.

    >
    > > I am looking to create XML. Xml is not too difficult so I thought to create it manually using ElementTree.

    >
    > > First I noted that ET.toString does escape <>& but not " and '

    >
    > > Is that normal?

    >
    > >

    >
    > " needs to be encoded when it's in an attribute's value:
    >
    >
    >
    > <tag value="a quote (&quot;)">



    OK I understood.

    >
    >
    > because it's also being used as a delimiter in that case, but elsewhere
    >
    > it has no special meaning.
    >
    >
    >
    > > Since I have also the need to sign the XML I need the ability to createxml but without xml escaping (unescaped data are signed).

    >
    >
    >
    > XML with the escaping isn't valid XML.
    >


    Of course I know it is not valid without escaping. But I need it only for signing. I will recheck this if really the web service wants the data to be signed as non escaped.

    Thanks
    Nenad
    , Dec 12, 2012
    #3
  4. Guest

    Il giorno martedì 11 dicembre 2012 20:59:54 UTC+1, MRAB ha scritto:
    > > Hello, I have posted the same in XML group but it seems pretty dead there so I will repost here.

    >
    > >

    >
    > > I am new to xml processing in python.

    >
    > > I am looking to create XML. Xml is not too difficult so I thought to create it manually using ElementTree.

    >
    > > First I noted that ET.toString does escape <>& but not " and '

    >
    > > Is that normal?

    >
    > >

    >
    > " needs to be encoded when it's in an attribute's value:
    >
    >
    >
    > <tag value="a quote (&quot;)">



    OK I understood.

    >
    >
    > because it's also being used as a delimiter in that case, but elsewhere
    >
    > it has no special meaning.
    >
    >
    >
    > > Since I have also the need to sign the XML I need the ability to createxml but without xml escaping (unescaped data are signed).

    >
    >
    >
    > XML with the escaping isn't valid XML.
    >


    Of course I know it is not valid without escaping. But I need it only for signing. I will recheck this if really the web service wants the data to be signed as non escaped.

    Thanks
    Nenad
    , Dec 12, 2012
    #4
  5. , 12.12.2012 03:19:
    > Il giorno martedì 11 dicembre 2012 20:59:54 UTC+1, MRAB ha scritto:
    >>
    >>> Since I have also the need to sign the XML I need the ability to create xml but without xml escaping (unescaped data are signed).

    >>
    >> XML with the escaping isn't valid XML.

    >
    > Of course I know it is not valid without escaping. But I need it only for signing. I will recheck this if really the web service wants the data to be signed as non escaped.


    If it expects non-XML, you should tell the owners of the web service so
    that they can fix it.

    Stefan
    Stefan Behnel, Dec 15, 2012
    #5
    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. Kee Nethery
    Replies:
    12
    Views:
    2,076
    Stefan Behnel
    Jun 27, 2009
  2. Stefan Behnel
    Replies:
    0
    Views:
    772
    Stefan Behnel
    May 4, 2010
  3. Barak, Ron
    Replies:
    1
    Views:
    1,162
    John Machin
    May 5, 2010
  4. Terry Reedy
    Replies:
    1
    Views:
    527
    John Machin
    May 5, 2010
  5. Doug
    Replies:
    2
    Views:
    485
Loading...

Share This Page