我一直在寻找写 @Entity
public class Transaction {
...
@Column(name="name", unique=true)
private String name;
...
}
,lxml
等xml文件的许多方法。但是,我没有想要的东西。让我从头开始。我选择使用minidom
。这是我的代码
xml.etree.ElementTree
from xml.etree.ElementTree import Element, SubElement, ElementTree, tostring
from xml.dom import minidom
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
rough_string = tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
Clip = Element('Clip')
tree = ElementTree(Clip)
Header = SubElement(Clip, 'Header')
Filename = SubElement(Header, 'Filename')
Filename.text = "C001101_001.mp4"
Duration = SubElement(Header, 'Duration')
Duration.text = "00:07:13"
Alarms = SubElement(Clip, 'Alarms')
Alarm = SubElement(Alarms, 'Alarm')
StartTime = SubElement(Alarm, "StartTime")
StartTime.text = "00:03:27"
AlarmDescription = SubElement(Alarm, "AlarmDescription")
AlarmDescription.text = "Loitering"
AlarmDuration = SubElement(Alarm, "AlarmDuration")
AlarmDuration.text = "00:00:44"
print(prettify(Clip))
tree.write(open("obbo.xml", "wb"))
将此打印到控制台上
print(prettify(Clip))
我想要的是将<?xml version="1.0" ?>
<Clip>
<Header>
<Filename>C001101_001.mp4</Filename>
<Duration>00:07:13</Duration>
</Header>
<Alarms>
<Alarm>
<StartTime>00:03:27</StartTime>
<AlarmDescription>Loitering</AlarmDescription>
<AlarmDuration>00:00:44</AlarmDuration>
</Alarm>
</Alarms>
</Clip>
文件准确地写成印刷形式的{strong> ,obbo.xml
确实写了,但是它是一行:
tree.write(open("obbo.xml", "wb"))
希望您能帮上忙〜