如何使用python 3将xmlstring附加到现有的xml文档

时间:2018-06-01 13:31:19

标签: xml elementtree

我想将下面的xml字符串附加到名为“output.xml”的现有xml文件中:

b'<Studends>\n  <Student MRID="ID1" name="Student1" Research_area="Physics of particles">\n    <Labo ID="POP" location="88C8"/>\n    <Labo ID="MAG" location="8500"/>\n  </Student>\n  <Student MRID="ID2" name="Student2" Research_area="Social networks">\n    <Labo ID="SN" location="8410"/>\n  </Student>\n  <Location ID="88C8" name="Accelerator lab"/>\n  <Location ID="8500" name="Magnetism"/>\n  <Location ID="8410" name="Social media"/>\n</Studends>'

此xml字符串已使用lxml etree构建。

我想将xml字符串附加到输出文件(当前包含编码和版本),同时遵循树层次结构,如下所示:

<Studends>
    <Student MRID="ID1" name="Student1" Research_area="Physics of particles">
        <Labo ID="POP" location="88C8"/>
        <Labo ID="MAG" location="8500"/>
    </Student>
    <Student MRID="ID2" name="Student2" Research_area="Social networks">
        <Labo ID="SN" location="8410"/>
    </Student>
    <Location ID="88C8" name="Accelerator lab"/>
    <Location ID="8500" name="Magnetism"/>
    <Location ID="8410" name="Social media"/>
</Studends>'

请指教。

0 个答案:

没有答案