File1.xml
<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
<qgis projectname="" version="2.6.1-Brighton">
<title></title>
<mapcanvas>
</mapcanvas>
</qgis>
File2.xml
<?xml version="1.0" ?>
<legend updateDrawingOrder="true">
<legendlayer checked="Qt::Checked" name="Grid1.shp" open="true" showFeatureCount="0">
<filegroup hidden="false" open="true">
<legendlayerfile isInOverview="0" layerid="Grid1_shp20150117231830763" visible="1"/>
</filegroup>
</legendlayer>
<legendlayer checked="Qt::Checked" name="world_countries.shp" open="true" showFeatureCount="0">
<filegroup hidden="false" open="true">
<legendlayerfile isInOverview="0" layerid="world_countries_shp20150117231830790" visible="1"/>
</filegroup>
</legendlayer>
</legend>
我需要File1.XML到File2.XML的一部分...在脚本下面打印所需的但仅用于元素。如何将此更新保存在File2.XML中?
from xml.etree import ElementTree as et
tree1 = xml.etree.ElementTree.parse('File1.xml')
root1 = tree1.getroot()
tree2 = xml.etree.ElementTree.parse('File2.xml')
root2 = tree2.getroot()
root2.insert(0,root1)
print et.tostring(root2)
目标软件需要以下格式的标签
<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
<qgis projectname="" version="2.6.1-Brighton">
<title></title>
<legend updateDrawingOrder="true">
<legendlayer checked="Qt::Checked" name="Grid1.shp" open="true" showFeatureCount="0">
<filegroup hidden="false" open="true">
<legendlayerfile isInOverview="0" layerid="Grid1_shp20150117231830763" visible="1"/>
</filegroup>
</legendlayer>
<legendlayer checked="Qt::Checked" name="world_countries.shp" open="true" showFeatureCount="0">
<filegroup hidden="false" open="true">
<legendlayerfile isInOverview="0" layerid="world_countries_shp20150117231830790" visible="1"/>
</filegroup>
</legendlayer>
</legend>
<mapcanvas>
</mapcanvas>
</qgis>
答案 0 :(得分:0)
但是,您已经构建和/或修改了元素树,可以使用write
方法将其保存到磁盘:
tree2.write('File2.xml')