开发者

stop minidom converting < > to &lt; &gt;

开发者 https://www.devze.com 2022-12-29 03:29 出处:网络
Im trying to output some data from my google app engine datastore to xml so that a flash file can read it,

Im trying to output some data from my google app engine datastore to xml so that a flash file can read it,

The problem is wh开发者_运维知识库en using CDATA tags the outputted xml contains &lt; instead of <

e.g

<name>&lt;![CDATA][name]]&gt;</name>

here is my python which outputs the xml:

    doc = Document()

    feed = doc.createElement("feed")
    doc.appendChild(feed)
    tags_element = doc.createElement("names")
    feed.appendChild(tags_element)
    copen = "<![CDATA]["
    cclose = "]]>"

    tags = db.GqlQuery("SELECT * FROM Tag ORDER BY date DESC")

    for tag in tags:
        tag_element = doc.createElement("name")
        tags_element.appendChild(tag_element)
        the_tag = doc.createTextNode("%s%s%s" % (copen,str(tag.thetag), cclose))
        tag_element.appendChild(the_tag)

    self.response.headers["Content-Type"] = "application/xml"
    self.response.out.write(doc.toprettyxml(indent="    "))

i know this is an encoding issue just can't seem to get to the route of the problem,

thanks in advance


It seems the createCDATASection method works for me.

for tag in tags:
    tag_element = doc.createCDATASection(tag.thetag)
    tags_element.appendChild(tag_element)


To do what you are attempting, you need to actually add a CDATA-block using the appropriate minidom methods. It's not an encoding issue, per se, but when you use createTextNode it encodes XML control characters to actual text characters for you, in order to be helpful, no doubt.


createTextNode converts reserved chars (< > &) into entities.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号