Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

xml - Python etree control empty tag format

When creating an XML file with Python's etree, if we write to the file an empty tag using SubElement, I get:

<MyTag />

Unfortunately, our XML parser library used in Fortran doesn't handle this even though it's a correct tag. It needs to see:

<MyTag></MyTag>

Is there a way to change the formatting rules or something in etree to make this work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As of Python 3.4, you can use the short_empty_elements argument for both the tostring() function and the ElementTRee.write() method:

>>> from xml.etree import ElementTree as ET
>>> ET.tostring(ET.fromstring('<mytag/>'), short_empty_elements=False)
b'<mytag></mytag>'

In older Python versions, (2.7 through to 3.3), as a work-around you can use the html method to write out the document:

>>> from xml.etree import ElementTree as ET
>>> ET.tostring(ET.fromstring('<mytag/>'), method='html')
'<mytag></mytag>'

Both the ElementTree.write() method and the tostring() function support the method keyword argument.

On even earlier versions of Python (2.6 and before) you can install the external ElementTree library; version 1.3 supports that keyword.

Yes, it sounds a little weird, but the html output mostly outputs empty elements as a start and end tag. Some elements still end up as empty tag elements; specifically <link/>, <input/>, <br/> and such. Still, it's that or upgrade your Fortran XML parser to actually parse standards-compliant XML!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...