我有一个格式为xml:
的Quickbird元数据<?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)
你能帮我解决一下如何完成这项任务吗?
非常感谢你的帮助。
答案 0 :(得分:0)
这应该打印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”查看结果)