【发布时间】:2015-03-16 03:41:46
【问题描述】:
文件1.xml
<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
<qgis projectname="" version="2.6.1-Brighton">
<title></title>
<mapcanvas>
</mapcanvas>
</qgis>
文件2.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>
【问题讨论】:
-
给出简短的示例输入和所需的输出。
-
我已编辑以提供示例
标签: python xml block elementtree addchild