Hello!
I need some help...

I parsed an xml file using the minidom.parse method, but when i
printed the document object to the screen (using toxml() function), i noticed that the parser automatically replaced my " (inverted commas) chars into ' chars.

for example :

<Father_Tag>
<Son_Tag title=' "Son" '>
This is an "example
</Son_Tag>
</Father_Tag>

turned out to be :

<Father_Tag>
<Son_Tag title=" &quot;Son&quot; ">
This is an &quot;example
</Son_Tag>
</Father_Tag>

as a result, the &quot; replaced the inverted commas (") that i added.
this shit really messes up my program!

can anyone help me?

Dani AI

Generated

Brief answer for , and a note to and : this is normal XML behavior, not a bug in Python.

When minidom.parse builds a DOM it stores attribute values as strings but does not keep which quote character you used in the source. When you call toxml()/writexml the serializer picks a quoting style (minidom uses double quotes) and escapes any double quotes inside an attribute value as the entity &quot; so the output remains well‑formed. The forum display showing &amp;quot; is just the page HTML escaping the & — the underlying XML contains &quot;, which XML parsers will decode back to a literal " when you read the document.

To see this locally, inspect the DOM value instead of the serialized text:

from xml.dom.minidom import Document
d = Document()
e = d.createElement('item')
e.setAttribute('title', 'He said "hello"')
d.appendChild(e)
print(repr(e.getAttribute('title')))  # shows the actual string with quotes
print(d.toxml())                       # shows the serialized form with &quot;

Practical options:

  • Keep it as an attribute and accept &quot; — any correct XML consumer will turn that entity back into " on parse.
  • Move the text into element content (or a CDATA section) if you need unescaped quotes visible in the file. CDATA affects element text only, not attributes.
  • If you absolutely must preserve the original source formatting (which quote character was used), do not parse/re-serialize the file — parsing loses that lexical detail, so use a text-based approach or a specialized round-trip tool.

Finally: backslash escapes (like \") are only language/string-literal escapes (Python, etc.). They do not act as XML escapes inside attribute values — use entity references for XML.

Recommended Answers

All 4 Replies

What is a &quot supposed to mean? Are you sure you are using PHP?

I think Avner doesn't want his double quotes replaced by single quotes? I have seen this happen with Python before, but it didn't make any difference. Other languages ar not quite so forgiving. The &quot is an HTML/XML thing.

yea but actually it's the opposite, my single quotes are replaced by double quotes, and the double quotes remains the same, so i can't really use double quotes as part of a string...

What would happen if you wrote ' \"Son\" ' rather than ' "Son" ' ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.