繁体   English   中英

python:使用不带类的xmlns解析xml文件

[英]python: parsing xml file with xmlns without class

下面是XML文件:

<Schedule xmlns="http://xmlns.xyz.com/fal/downtimeschedule/V1.0">
 <Downtime end="20181020000" id="10001197610_20181027000_201810000" mode="cold" start="20181020000"/>
 <PODS>
  <POD DC="US - Washing" deferUpgrade="true" name="ABCD" patching="Production" EndTime="20181028040000">
   <CR id="12345"/>
   <CR id="12346"/>
   <CR id="123"/>
  </POD>
  <POD DC="US - Washing" deferUpgrade="true" name="ABCD-TEST" patching="Production" EndTime="20181028040000">
   <CR id="12345"/>
   <CR id="12346"/>
   <CR id="123"/>
  </POD>
 </PODS>

我想在带有列的csv文件中提供输出

1> POD(name)
2> POD(DC)
3> POD(deferUpgrade)

有人可以帮忙吗? 我可以在生产环境中使用库xml.etree.ElementTree吗?

下面是代码。

#!/usr/bin/python
import os
import pandas as pd
import requests
import xml.etree.ElementTree as ET

tree = ET.parse('schedule.xml')

root = tree.getroot()

print root
print root.findall('PODS')
for pods in root.iter('Schedule'):
 for pod in pods.iter('POD'):
    print pod.get('name')

输出:

<Element '{http://xmlns.oracle.com/falcm/flo/downtimeschedule/V1.0}Schedule' at 0x2ae6ed0>
[]
from xml.dom import minidom

def parseXml(filePath):

    xmldoc = minidom.parse(filePath)
    PODs= xmldoc.getElementsByTagName("POD")
    name=[]
    DC=[]
    deferUpgrade=[]
    for pod in PODs:
        if pod.hasAttribute("name") and pod.hasAttribute("DC") and pod.hasAttribute("deferUpgrade"):
            name.append(pod.getAttribute("name"))
            DC.append(pod.getAttribute("DC"))
            deferUpgrade.append(pod.getAttribute("deferUpgrade"))
    return(name,DC,deferUpgrade)

您可以使用名称,DC,deferUpgrade来创建CSV文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM