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

  • SEARCH
  • Home
  • 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 803035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:41:42+00:00 2026-05-14T23:41:42+00:00

I have a collection of nodes xml.dom.Node objects created using xml.dom.minidom. I store them

  • 0

I have a collection of nodes xml.dom.Node objects created using xml.dom.minidom. I store them (individually) in a database by converting them to a string using the toxml() method of a the Node object.

The problem is that I’d sometimes like to be able to convert them back to the appropriate Node object using a parser of some kind. As far as I can see the various libraries shipped with python use Expat which won’t parse a string like ” or indeed anything which is not a correct xml string.

So, does anyone have any ideas? I realise I could pickle the nodes in some way and then unpickle them, but that feels unpleasant and I’d much rather be storing in a form I can read for maintenance purposes. Surely there is something that will do this?

In response to the doubt expressed that this is possible, an example of what I mean:

>>> import xml.dom.minidom
>>> x=xml.dom.minidom.parseString('<a>foo<b>thing</b></a>')
>>> x.documentElement.childNodes[0]
<DOM Text node "u'foo'">
>>> x.documentElement.childNodes[0].toxml()
u'foo'
>>> xml.dom.minidom.parseString(x.documentElement.childNodes[0].toxml())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/dom/minidom.py", line 1928, in parseString
    return expatbuilder.parseString(string)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/dom/expatbuilder.py", line 940, in parseString
return builder.parseString(string)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/xml/dom/expatbuilder.py", line 223, in parseString
parser.Parse(string, True)
xml.parsers.expat.ExpatError: syntax error: line 1, column 0

In other words the “.toxml()” method does not create something that Expat (and hence out of the box parseString) will parse.

What I would like is something that will parse u’foo’ into a text node. I.e. something that will reverse the effect of .toxml()

  • 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-14T23:41:42+00:00Added an answer on May 14, 2026 at 11:41 pm

    What types of node do you need to store?

    Obviously Element nodes should just work if serialised with .toxml('utf-8'); the results should be parseable as an XML document as-is and the element retrievable from documentElement, as long as there are no EntityReferences inside it that would need definition in the doctype.

    Text nodes, on the other hand, would need either HTML-decoding or some wrapping to parse. If you only needed elements and text nodes you could guess whether it was an element from the first character, since that must always be < for an element:

    var xml= node.toxml('utf-8')
    
    ...
    
    if (xml.startswith('<')):
        node= minidom.parseString(xml).documentElement
    else:
        node= minidom.parseString('<x>%s</x>'%xml).documentElement.firstChild
    

    Comment nodes could similarly be stored by checking for <!--.

    Other node types like Attr would be more work since their XML representation is not easily distinguishable from Text. You would probably need to store an out-of-band nodeType value to remember it. OTOH minidom doesn’t implement toxml() on Attr anyway so maybe that’s not an issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.