6

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!

Community
  • 1
  • 1
Kimbluey
  • 1,199
  • 2
  • 12
  • 23
  • 1
    @VivekSable Haha thank you :) I'm a tad OCPD – Kimbluey Mar 02 '15 at 18:27
  • 2
    can u check `>>> root = ET.Element("databaseConfiguration", {"xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance", "xsi:noNamespaceSchemaLocation":"database.xsd"}) ` is working or not? – Vivek Sable Mar 02 '15 at 18:41
  • 1
    @VivekSable Yes! That definitely worked. I was trying to use those curly brackets before but wasn't sure of the syntax. I'm not sure which implementation is better - this one or Daniel's? – Kimbluey Mar 02 '15 at 18:45
  • 1
    yes, I also do not no much about this. I read from the http://lxml.de/tutorial.html , can u look this site also? – Vivek Sable Mar 02 '15 at 18:50
  • @VivekSable Thanks! I see what it's doing now. I still don't know what the asterisks in Daniel's answer are doing though. Oh well, they both seem to work – Kimbluey Mar 02 '15 at 18:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72083/discussion-between-vivek-sable-and-kim-arundale). – Vivek Sable Mar 02 '15 at 18:56

2 Answers2

7

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"})
>>> 
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56
3

Simply use a dictionary

root = ET.Element("databaseConfiguration", **{'xmlns:xsi':"http://www.w3.org/2001/XMLSchema-instance",
               'xsi:noNamespaceSchemaLocation':"database.xsd"})
Daniel
  • 42,087
  • 4
  • 55
  • 81
  • Hi Daniel, what is the difference between your answer and what Vivek suggested: `>>>root = ET.Element("databaseConfiguration", {"xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance", "xsi:noNamespaceSchemaLocation":"database.xsd"})` **e.g. what do the extra asterisks do?** – Kimbluey Mar 02 '15 at 18:47