Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 237647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:26:31+00:00 2026-05-11T20:26:31+00:00

I’m trying to serialize some data to xml in a way that can be

  • 0

I’m trying to serialize some data to xml in a way that can be read back. I’m doing this by manually building a DOM via xml.dom.minidom, and writing it to a file using the included writexml method.

Of particular interest is how I build the text nodes. I do this by initializing a Text object and then setting its data attribute. I’m not sure why the Text object doesn’t take its content in the constructor, but that’s just the way it in simplemented in xml.dom.minidom.

To give a concrete example, the code looks something like this:

import xml.dom.minidom as dom
e = dom.Element('node')
t = dom.Text()
t.data = "The text content"
e.appendChild(t)
dom.parseString(e.toxml())

This seemed reasonable to me, particularly since createTextNode itself is implemented exactly like this:

def createTextNode(self, data):
    if not isinstance(data, StringTypes):
        raise TypeError, "node contents must be a string"
    t = Text()
    t.data = data
    t.ownerDocument = self
    return t

The problem is that setting the data like this allows us to write text that later cannot be parsed back. To give an example, I am having difficulty with the following character:

you´ll

The quote is ord(180), ‘\xb4’. My question is, what is the correct procedure to encode this data into an xml document suck that I parse the document with minidom to restore the original tree?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:26:31+00:00Added an answer on May 11, 2026 at 8:26 pm

    The issue you’re encountering, as explained in Python’s online docs, is that of Unicode encoding:

    Node.toxml([encoding])
    Return the XML that the DOM represents as a string.
    
    With no argument, the XML header does not specify an encoding, and the result is
    Unicode string if the default encoding cannot represent all characters in the 
    document. Encoding this string in an encoding other than UTF-8 is likely
    incorrect, since UTF-8 is the default encoding of XML.
    
    With an explicit encoding [1] argument, the result is a byte string in the 
    specified encoding. It is recommended that this argument is always specified.
    To avoid UnicodeError exceptions in case of unrepresentable text data, the 
    encoding argument should be specified as “utf-8”.
    

    So, call .toxml('utf8'), not just .toxml(), and use unicode strings as text contents, and you should be fine for a “round-trip” as you desire. For example:

    >>> t.data = u"The text\u0180content"
    >>> dom.parseString(e.toxml('utf8')).toxml('utf8')
    '<?xml version="1.0" encoding="utf8"?><node>The text\xc6\x80content</node>'
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 178k
  • Answers 178k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Why are Production and Sales are in different schemas? On… May 12, 2026 at 3:38 pm
  • Editorial Team
    Editorial Team added an answer You could run a cronjob that would generate the weather… May 12, 2026 at 3:38 pm
  • Editorial Team
    Editorial Team added an answer Well, here are those different options in code: Option 1:… May 12, 2026 at 3:38 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.