-1

Would like to write raw string "&quot" to value of an attribute.

using python minidom!

After writing "&quot" to XML file ,it looks like "&ampquot" in the file.

pls help me on this....

  • Possible duplicate of [How do I escape ampersands in XML?](http://stackoverflow.com/questions/1328538/how-do-i-escape-ampersands-in-xml) – Rohan Khude Jul 25 '16 at 06:39
  • I just created an answer, but now I have a question. Do you literally want `&quot`, or do you actually want `"` (which is how a double quote character is encoded in an XML string). If you really do want `&quot`, then it seems like `&quot` is in fact how it *should* appear in the file, because `&` represents the ampersand character. – Warren Weckesser Jul 25 '16 at 06:44

1 Answers1

0

Use the regular double quote character in the string value of the attribute. The code will take care of converting it to " in the XML output.

Here's an example. First, create the document:

In [52]: from xml.dom.minidom import getDOMImplementation

In [53]: impl = getDOMImplementation()

In [54]: doc = impl.createDocument(None, "tag", None)

In [55]: top_element = doc.documentElement

Set the "foo" attribute of the top element. Note that there are double quotes in the string that will be the value of the "foo" attribute.

In [56]: top_element.setAttribute("foo", '"plate of shrimp"')

When the document is printed, those double quotes are converted to ":

In [57]: print(top_element.toprettyxml())
<tag foo="&quot;plate of shrimp&quot;"/>
Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214