Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python(elementtree)中向musicXML树添加新元素?_Python_Xml_Elementtree_Musicxml - Fatal编程技术网

如何在Python(elementtree)中向musicXML树添加新元素?

如何在Python(elementtree)中向musicXML树添加新元素?,python,xml,elementtree,musicxml,Python,Xml,Elementtree,Musicxml,我正在使用Python批量编辑许多musicXML文件,这些文件目前看起来如下所示: <score-partwise> ... <attributes> <transpose> <diatonic>-5</diatonic> <chromatic>-9</chromatic> </transpose>

我正在使用Python批量编辑许多musicXML文件,这些文件目前看起来如下所示:

    <score-partwise>
    ...
      <attributes>
        <transpose>
          <diatonic>-5</diatonic>
          <chromatic>-9</chromatic>
          </transpose>
        </attributes>
    ...
      </score-partwise>

...
-5
-9
...
如何在
中添加
-1
,如下所示

    <score-partwise>
    ...
      <attributes>
        <transpose>
          <diatonic>-5</diatonic>
          <chromatic>-9</chromatic>
          <octave-change>-1</octave-change>
          </transpose>
        </attributes>
    ...
      </score-partwise>

...
-5
-9
-1
...
我尝试过这样做:

import xml.etree.ElementTree as ET

attributes   = ET.Element("attributes")
attributes.append(ET.fromstring('<transpose><octave-change>-1</octave-change></transpose>'))
将xml.etree.ElementTree作为ET导入
属性=ET.Element(“属性”)
attributes.append(ET.fromstring('-1'))
没有成功


非常感谢您的帮助。谢谢。

只需找到元素并附加:

x = """<score-partwise>    
      <attributes>
        <transpose>
          <diatonic>-5</diatonic>
          <chromatic>-9</chromatic>
          </transpose>
        </attributes>    
      </score-partwise>"""

import xml.etree.ElementTree as et
xml = et.fromstring(x)

#
xml.find("attributes").append(et.fromstring('<transpose><octave-change>-1</octave-change></transpose>'))

print(et.tostring(xml))
如果您想要格式化,您可能会发现lxml是一个更好的选择:

将lxml.etree作为et导入

parser = et.XMLParser(remove_blank_text=True)
xml = et.parse("test.xml",parser)


xml.find(".//attributes/transpose").append(et.fromstring('<octave-change>-1</octave-change>'))
xml.write('test.xml', pretty_print=True)
parser=et.XMLParser(删除\u blank\u text=True)
xml=et.parse(“test.xml”,解析器)
xml.find(“.//attributes/transpose”).append(et.fromstring('-1'))
write('test.xml',pretty\u print=True)
其中将写:

<score-partwise>
  <attributes>
    <transpose>
      <diatonic>-5</diatonic>
      <chromatic>-9</chromatic>
      <octave-change>-1</octave-change>
    </transpose>
  </attributes>
</score-partwise>

-5
-9
-1

查看运行代码时产生的输出、预期结果以及一个小示例可能会很有帮助,这样其他人就可以重现您的问题。@TheLoverter4865一旦我得到完整的代码,我将用完整的代码更新问题。该程序的目的是一次更改数百个musicXML文件中的某些元数据字段。因此,如果一个目录中有100个文件用于“长笛”,用户可以使用此程序更改所需目标乐器(例如“双簧管”)的相关XML元数据。最终程序会编写新文件。上面的代码只是整个程序的一部分。我可以使用
tree.find(“.//attributes/transpose”).append(et.fromstring('-1'))
where
tree=et.parse(I),而不是使用
xml
中的变量
i
目录中的每个文件都是通过for循环访问的吗?让我自己试试,我会报告:)
tree.find(“.//attributes/transpose”).append(et.fromstring('-1'))
确实是根据我在第一条评论中定义的
tree
工作的。谢谢你的帮助。
<score-partwise>
      <attributes>
        <transpose>
          <diatonic>-5</diatonic>
          <chromatic>-9</chromatic>
          <octave-change>-1</octave-change></transpose>
        </attributes>
</score-partwise>
xml = et.fromstring(x)

print(et.tostring(xml))
e = et.SubElement(xml.find(".//attributes/transpose"), "octave-change")
e.text = "-1"
e.tail= "\n"
parser = et.XMLParser(remove_blank_text=True)
xml = et.parse("test.xml",parser)


xml.find(".//attributes/transpose").append(et.fromstring('<octave-change>-1</octave-change>'))
xml.write('test.xml', pretty_print=True)
<score-partwise>
  <attributes>
    <transpose>
      <diatonic>-5</diatonic>
      <chromatic>-9</chromatic>
      <octave-change>-1</octave-change>
    </transpose>
  </attributes>
</score-partwise>