我有Quickbird的元数据格式为xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<isd>
<IMD>
<VERSION>AA</VERSION>
<GENERATIONTIME>2008-01-04T18:36:17.000000Z</GENERATIONTIME>
<PRODUCTORDERID>005708443040_01_P001</PRODUCTORDERID>
<PRODUCTCATALOGID>901001001E9ED900</PRODUCTCATALOGID>
</IMD>
</isd>
我想把xml转换成文本格式,如下所示:
version = "AA"; generationTime = 2008-01-04T18:36:17.000000Z; productOrderId = "005708443040_01_P001"; productCatalogId = "901001001E9ED900"; childCatalogId = "202001001E9ED800";
我写的python代码如下,但它没有提供我期望的结果
from xml.dom import minidom
xmldoc = minidom.parse("image.XML")
isd = xmldoc.getElementsByTagName("isd")[0]
imds = isd.getElementsByTagName("IMD")
for imd in imds:
print (imd)
你能告诉我如何做这个任务吗?
非常感谢你的帮助
这将打印XML的所有内容(它不会像您期望的那样转换为骆驼式大小写,因为无法知道哪些字符要大写,哪些字符要小写):
from xml.dom import minidom
xmldoc = minidom.parse("image.XML")
isd = xmldoc.getElementsByTagName("isd")[0]
imds = isd.getElementsByTagName("IMD")
for imd in imds:
for child in imd.childNodes:
if child.nodeType == minidom.Node.ELEMENT_NODE:
print child.nodeName+ ' = "' + child.childNodes[0].nodeValue + '"; ',
这将打印:
VERSION = "AA"; GENERATIONTIME = "2008-01-04T18:36:17.000000Z"; PRODUCTORDERID = "005708443040_01_P001"; PRODUCTCATALOGID = "901001001E9ED900";
查看下面的工作示例Python Fiddle(点击"RUN"查看结果)