4

我有一个现有的 xml 文件,如下所示:

<vehicleTravelTimeMeasurements>
        <vehicleTravelTimeMeasurement name="ckkkkkkkkkk" no="2">
            <start link="1" pos="3.864983"/>
            <end link="3" pos="23.275375"/>
        </vehicleTravelTimeMeasurement>
        <vehicleTravelTimeMeasurement name="" no="3">
            <start link="1" pos="3.864983"/>
            <end link="2" pos="13.275375"/>
        </vehicleTravelTimeMeasurement>
</vehicleTravelTimeMeasurements>

我正在尝试使用 xml.etree.cElementTree 构建与上图相同的格式,并将它们正确添加到 xml 中:

new = ET.Element("vehicleTravelTimeMeasurement", name = "kkk", no = "4")
newsub1 =  ET.Element("start", link = "1", pos="3.88888")
newsub2 = ET.Element("end",link = "3", pos = "3.88888")

有人可以帮我解决这个问题吗?

先感谢您!

4

2 回答 2

4

您想使用and元素SubElement并将其添加到元素中。然后在位置 2 插入新创建的元素,因为 0 和 1 已经被占用。startendvehicleTravelTimeMeasurement

import xml.etree.ElementTree as ET

def indent(elem, level=0):
    i = "\n" + level*"  "
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "  "
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

xml = '''<vehicleTravelTimeMeasurements>
        <vehicleTravelTimeMeasurement name="ckkkkkkkkkk" no="2">
            <start link="1" pos="3.864983"/>
            <end link="3" pos="23.275375"/>
        </vehicleTravelTimeMeasurement>
        <vehicleTravelTimeMeasurement name="" no="3">
            <start link="1" pos="3.864983"/>
            <end link="2" pos="13.275375"/>
        </vehicleTravelTimeMeasurement>
</vehicleTravelTimeMeasurements>'''

root = ET.fromstring(xml)
new = ET.Element("vehicleTravelTimeMeasurement", name = "kkk", no = "4")
newsub1 = ET.SubElement(new, "start", link = "1", pos="3.88888")
newsub2 = ET.SubElement(new, "end",link = "3", pos = "3.88888")
root.insert(2, new)
indent(root)
with open('test.xml', 'w') as f:
    f.write(ET.tostring(root))
于 2015-12-31T16:45:34.153 回答
0

短版/更新和编辑:

您唯一需要做的就是 (A) 添加newsub1和添加newsub2new. 和 (B) 添加newroot.

root = ET.fromstring(xml)  # or whichever way you're getting `root`
# these 3 lines form your code:
new = ET.Element('vehicleTravelTimeMeasurement', name="kkk", no="4")
newsub1 = ET.Element('start', link='1', pos='3.88888')
newsub2 = ET.Element('end', link='3', pos='3.88888')
# the next steps to add
new.append(newsub1)
new.append(newsub2)
root.append(new)

请注意,(A)和(B)可以按任何顺序完成,并且可以缩短,如下所示:

>>> root = ET.fromstring(xml)
>>> new = ET.Element('vehicleTravelTimeMeasurement', name="kkk", no="4")
>>> root.append(new)  # note that I've immediately added `new`
>>> ET.SubElement(new, 'start', link='1', pos='3.88888')
<Element 'start' at 0x24707b8>
>>> ET.SubElement(new, 'end', link='3', pos='3.88888')
<Element 'end' at 0x24ea978>
>>> # there's no need to store the subelements in `newsub1` and
... # `newsub2` if you don't need to do anything with them
...
>>> indent(root)
>>> print ET.tostring(root)
<vehicleTravelTimeMeasurements>
  <vehicleTravelTimeMeasurement name="ckkkkkkkkkk" no="2">
    <start link="1" pos="3.864983" />
    <end link="3" pos="23.275375" />
  </vehicleTravelTimeMeasurement>
  <vehicleTravelTimeMeasurement name="" no="3">
    <start link="1" pos="3.864983" />
    <end link="2" pos="13.275375" />
  </vehicleTravelTimeMeasurement>
  <vehicleTravelTimeMeasurement name="kkk" no="4">
    <start link="1" pos="3.88888" />
    <end link="3" pos="3.88888" />
  </vehicleTravelTimeMeasurement>
</vehicleTravelTimeMeasurements>

笔记:

  1. 我在创建它之后添加newroot它。
  2. 如果您知道自己总是在追加,请使用append而不是insert,而不是需要跟踪no原始 xml 中的 's
    • 除非你无论如何都需要阅读它来计算下一个' no'属性
  3. ET.SubElement(new)即使已经附加了更新new(和)。rootnew
  4. 如果您不需要对它们做任何事情,则无需 newsub1将子元素存储在其中。newsub2
    • 第一种方法(如您的方法)创建元素,然后将它们添加到 root 或 new。
    • 第二种方法用于ET.SubElement(new, ...)将元素添加到其父级。
  5. 函数indent来自这里,它引用了这个来源

上面的 Re # 4.2,也可以这样做:

root = ET.fromstring(xml)
new = ET.SubElement(root, 'vehicleTravelTimeMeasurement', name="kkk", no="4")
# `new` is already created as a subelement of `root` and appended
ET.SubElement(new, 'start', link='1', pos='3.88888')
ET.SubElement(new, 'end', link='3', pos='3.88888')

子元素文档

此函数创建一个元素实例,并将其附加到现有元素。

(强调我的)

于 2015-12-31T16:46:14.583 回答