How do I escape colons in an attribute name with Python's ElementTree?

Kimbluey

Background

I am using ElementTree in Python version 2.6 to create an XML file (using data retrieved from a database).

Code

The following line of code is the problem area, as I keep getting a syntax error because of the colons within my attribute names.

# Please ignore any errors the "^" characters would cause if they were
# actually part of my code - just using them as placeholders.

root = ET.Element("databaseConfiguration", xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance",
                                                ^
                  xsi:noNamespaceSchemaLocation="database.xsd")
                     ^

Question

What is the most efficient way to escape the colons in these attribute names in order to have root equivalent to the following:

<databaseConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="database.xsd"/>

Notes

I've looked at a few solutions on Stack Overflow (e.g. solution1, solution2, solution3 and solution4) where users were parsing an XML file, but I cannot seem to interpret these fixes as ones that would work for writing to an XML.



Thanks in advance!

Vivek Sable

may be following will work for you. Read from the link

>>> root = ET.Element("databaseConfiguration", {"xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance", "xsi:noNamespaceSchemaLocation":"database.xsd"})
>>> 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I retrieve an attribute's name using the nodeName property?

From Dev

How do I get Python's ElementTree to pretty print to an XML file?

From Dev

How do I output an XML file using ElementTree in python?

From Dev

How do I add attributes to SubElement in ElementTree (Python)

From Dev

How can I make Python's ElementTree enforce XML schema?

From Dev

How do I escape an underscore that follows a macro name?

From Dev

How do I name metadata for mp3's with python?

From Dev

How do I name metadata for mp3's with python?

From Dev

How do I print a url path in an `a` tag using the url's name attribute?

From Dev

How do I split a string with multiple commas and colons in javascript?

From Dev

How do I search Outlook messages for colons, spaces or parentheses?

From Dev

I want to read attribute value of attribute "name" of Tag <Attribute> in which attribute "class" is present.. how do i do that?

From Dev

How do I set a Python builtin Exception's message attribute when I raise it

From Dev

How do I set a Python builtin Exception's message attribute when I raise it

From Dev

How do I parse XML with attribute in python?

From Dev

How do I use CSS to select for a tag with arbitrary name attribute?

From Dev

How do I use CSS to select for a tag with arbitrary name attribute?

From Dev

How do I get a name of an attribute in the request in string format

From Dev

How do I access data attribute of elements with the same class name?

From Dev

How do I escape a colon in Python format() when using kwargs?

From Dev

How do I convert a string to an escape sequence in Python?

From Dev

How do I take Someone's name in Python and create a file to write to with that name?

From Dev

how do i combine the children of a node into one cell of a csv cell using elementtree in python?

From Dev

How to escape variable name in PHP's Heredoc?

From Dev

How do I change the value of an object's attribute that's a reference to another object's attribute?

From Dev

Why do I need to escape < and & when rendering an attribute?

From Dev

In Oracle SQL how do I escape hyphens in a column name or extract a column by its position?

From Dev

Python XML parsing with ElementTree: How to find values of elements with the same name?

From Dev

How do I display an element's attribute based on complex condition?

Related Related

  1. 1

    How do I retrieve an attribute's name using the nodeName property?

  2. 2

    How do I get Python's ElementTree to pretty print to an XML file?

  3. 3

    How do I output an XML file using ElementTree in python?

  4. 4

    How do I add attributes to SubElement in ElementTree (Python)

  5. 5

    How can I make Python's ElementTree enforce XML schema?

  6. 6

    How do I escape an underscore that follows a macro name?

  7. 7

    How do I name metadata for mp3's with python?

  8. 8

    How do I name metadata for mp3's with python?

  9. 9

    How do I print a url path in an `a` tag using the url's name attribute?

  10. 10

    How do I split a string with multiple commas and colons in javascript?

  11. 11

    How do I search Outlook messages for colons, spaces or parentheses?

  12. 12

    I want to read attribute value of attribute "name" of Tag <Attribute> in which attribute "class" is present.. how do i do that?

  13. 13

    How do I set a Python builtin Exception's message attribute when I raise it

  14. 14

    How do I set a Python builtin Exception's message attribute when I raise it

  15. 15

    How do I parse XML with attribute in python?

  16. 16

    How do I use CSS to select for a tag with arbitrary name attribute?

  17. 17

    How do I use CSS to select for a tag with arbitrary name attribute?

  18. 18

    How do I get a name of an attribute in the request in string format

  19. 19

    How do I access data attribute of elements with the same class name?

  20. 20

    How do I escape a colon in Python format() when using kwargs?

  21. 21

    How do I convert a string to an escape sequence in Python?

  22. 22

    How do I take Someone's name in Python and create a file to write to with that name?

  23. 23

    how do i combine the children of a node into one cell of a csv cell using elementtree in python?

  24. 24

    How to escape variable name in PHP's Heredoc?

  25. 25

    How do I change the value of an object's attribute that's a reference to another object's attribute?

  26. 26

    Why do I need to escape < and & when rendering an attribute?

  27. 27

    In Oracle SQL how do I escape hyphens in a column name or extract a column by its position?

  28. 28

    Python XML parsing with ElementTree: How to find values of elements with the same name?

  29. 29

    How do I display an element's attribute based on complex condition?

HotTag

Archive