By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,002 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,002 IT Pros & Developers. It's quick & easy.

Add encoding to XML element

P: 1
Hello

I've been working on a script that generates a XML document, and output needs to have encoding "ISO-8859-1" defined in the first <?xml ...> tag.

I found this example on http://infohost.nmt.edu/tcc/help/pubs/pyxml/creating.html

Expand|Select|Wrap|Line Numbers
  1. import xml.dom.minidom
  2. #import xml.dom.ext as domExt
  3.  
  4. dom = xml.dom.minidom.getDOMImplementation()
  5.  
  6. doctype = dom.createDocumentType("html",
  7.               "-//W3C//DTD XHTML 1.0 Strict//EN",
  8.               "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" )
  9.  
  10. doc = dom.createDocument( None, "html", doctype )
  11. (... snip ...)
  12.  
This should produce something like
Expand|Select|Wrap|Line Numbers
  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4. <html/>
  5.  
However, when I try it, the encoding part is not generated. Also, AFAIK "UTF-8" is the default coding and I require something else...

So how do you set what encoding to use?
Nov 21 '06 #1
Share this Question
Share on Google+
1 Reply


bartonc
Expert 5K+
P: 6,596
I don't have any experience, but section 8 of the 2.5 docs have some interesting looking tidbits like:

8.4 htmlentitydefs -- Definitions of HTML general entities

This module defines three dictionaries, name2codepoint, codepoint2name, and entitydefs. entitydefs is used by the htmllib module to provide the entitydefs member of the HTMLParser class. The definition provided here contains all the entities defined by XHTML 1.0 that can be handled using simple textual substitution in the Latin-1 character set (ISO-8859-1)

entitydefs A dictionary mapping XHTML 1.0 entity definitions to their replacement text in ISO Latin-1.
Nov 21 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.