Does python's minidom support Chinese?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anthony Liu

    Does python's minidom support Chinese?

    The following 4 lines of code parses an XML document
    very well if the XML document contains only English
    words.

    But when I insert one Chinese character into the XML
    document, then Python starts to complain when it hits
    the Chinese character, saying that it is an invalid
    token and thus it is not well-formed.

    This is the complaint of Python:

    ExpatError: not well-formed (invalid token): line 3,
    column 7

    line 3 and column 7 exactly pinpoints the 1st Chinese
    character in the XML document.

    The problem remains even if I try encoding="UTF-16" or
    encoding="GB231 2" or encoding="GBK" in the xml
    document.

    Note that GB2312 and GBK are Chinese encodings.

    Please give a hint. Thanks a lot!

    The 4 lines of code I used is here:

    # -*- coding: cp936 -*-
    from xml.dom import minidom
    xmldoc = minidom.parse(' test.xml')
    print xmldoc.toxml()



    _______________ _______________ ____
    Do you Yahoo!?
    Yahoo! Search - Find what you’re looking for faster


  • Uche Ogbuji

    #2
    Re: Does python's minidom support Chinese?

    Anthony Liu <antonyliu2002@ yahoo.com> wrote in message news:<mailman.2 50.1078955721.1 9534.python-list@python.org >...[color=blue]
    > The following 4 lines of code parses an XML document
    > very well if the XML document contains only English
    > words.
    >
    > But when I insert one Chinese character into the XML
    > document, then Python starts to complain when it hits
    > the Chinese character, saying that it is an invalid
    > token and thus it is not well-formed.
    >
    > This is the complaint of Python:
    >
    > ExpatError: not well-formed (invalid token): line 3,
    > column 7
    >
    > line 3 and column 7 exactly pinpoints the 1st Chinese
    > character in the XML document.[/color]

    This is an XML problem on your end, not a minidom problem. That error
    probably means that you are either omitting the XML declaration (and
    thus defaulting to UTF-8 or UTF-16) or declaring a bogus encoding.

    [color=blue]
    > The problem remains even if I try encoding="UTF-16" or
    > encoding="GB231 2" or encoding="GBK" in the xml
    > document.[/color]

    Well, you can't just go shopping about for oare it accordingly.

    Back to minidom: even after you fix your XML problems you may still
    have trouble with minidom because the expat reader has to understand
    the encoding you're using. I think that it may use the Python codecs
    model to find the encoding you declared, so you may just need to
    install a Python Chinese codecs package, and you'll be all set. I'm
    not entirely sure this si the case, though.


    --Uche
    Igbo-American immigrant from Nigeria, settled near Boulder, Colorado with my wife, three sons and daughter. Restless mind in a restless body, I do a million things without getting very much truly done

    Comment

    Working...