1

I'm using python to create xml files, and I need to create the attribute xml:id

how can I do this?

2
  • Do you mean something like this <?xml version="1.0" ?> <root> <doc xml:id="value">Some text</doc> </root> Commented Apr 4, 2016 at 14:56
  • yes exactly, sorry I should've specified. Commented Apr 4, 2016 at 15:03

1 Answer 1

1

Try using the xml.dom.minidom module

from xml.dom import minidom

doc = minidom.Document()

ele = doc.createElement("element")
doc.appendChild(ele)
ele.attributes['xml:id']= "something"

print (ele.toxml())
Sign up to request clarification or add additional context in comments.

7 Comments

well, I'm using lxml, and I specifically chose it because I need to create something like: <doc/>tail . Is this possible using dom??
do you mean a closing tag </doc> ?
no, I actually have tags like this <doc/> , just like <br/> in html
like this <element xml:id="something"/>
I have updated the answer, so try if it works for you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.