我在使用Python解析XML字符串时遇到问题

时间:2019-07-18 03:56:56

标签: python xml elementtree

我在使用python解析xml表时遇到问题。我是解析这些文件类型的新手。我目前正在使用ElementTree库。我想使用for循环为“ UsedPartTab”表的每一行获取特定属性。更具体地说,我想将以下属性分配给变量... PartNo,UsedQty,InvoiceNo。对我来说,最好的方法是什么?

我尝试索引根文档中的特定标签没有成功。我也尝试过findall,find和get方法。

    def GetPartsUsedList():
        request = GET_TICKET_INFO
        response = requests.post(url=GSPN_URL, verify=False, proxies=proxies, headers={"content-type": "text/xml;charset=UTF-8", "SOAPAction": '"www.samsungasc.com/GetTicketInfo"'}, data=request)
        xml = ET.fromstring(response.text)
        root = xml[0][0][0]
        print(root.text)

GetPartsUsedList()

<?xml version="1.0" encoding="utf-8" ?>
<rootdoc>
    <RetCode>0</RetCode>
    <ErrMsg></ErrMsg>
    <Carrier></Carrier>
    <CarrierName></CarrierName>
    <TicketNo>4149142579</TicketNo>
    <SCode1>01</SCode1>
    <SCode2></SCode2>
    <SCode3></SCode3>
    <SCode4>XC</SCode4>
    <IrisDefectCode>ARCS</IrisDefectCode>
    <IrisRepairCode>DPRT</IrisRepairCode>
    <AlertMessage></AlertMessage>
    <HighRisk></HighRisk>
    <Table>
        <Name>UsedPartTab</Name>
        <Column>
            <Name>SeqNo</Name>
            <Type>String</Type>
        </Column>
        <Column>
            <Name>PartStatus</Name>
            <Type>String</Type>
        </Column>
        <Column>
            <Name>PartNo</Name>
            <Type>String</Type>
        </Column>
        <Column>
            <Name>Description</Name>
            <Type>String</Type>
        </Column>
        <Column>
            <Name>Location</Name>
            <Type>String</Type>
        </Column>
        <Column>
            <Name>UsedQty</Name>
            <Type>Decimal</Type>
        </Column>
        <Column>
            <Name>RequestNo</Name>
            <Type>String</Type>
        </Column>
        <Column>
            <Name>PONo</Name>
            <Type>String</Type>
        </Column>
        <Column>
            <Name>SONo</Name>
            <Type>String</Type>
        </Column>
        <Column>
            <Name>InvoiceNo</Name>
            <Type>String</Type>
        </Column>
        <Column>
            <Name>InvoiceItemNo</Name>
            <Type>String</Type>
        </Column>
        <Column>
            <Name>DefectSerialNo</Name>
            <Type>String</Type>
        </Column>
        <Column>
            <Name>POStatus</Name>
            <Type>String</Type>
        </Column>
        <Column>
            <Name>TrackingNo</Name>
            <Type>String</Type>
        </Column>
        <Column>
            <Name>TrackingHyperLink</Name>
            <Type>String</Type>
        </Column>
        <Column>
            <Name>PostGoodsIssue</Name>
            <Type>String</Type>
        </Column>
        <ROW>
            <SeqNo>0001</SeqNo>
            <PartStatus>Used</PartStatus>
            <PartNo>0M75P-21-ESGN</PartNo>
            <Description></Description>
            <Location></Location>
            <UsedQty>1</UsedQty>
            <RequestNo></RequestNo>
            <PONo></PONo>
            <SONo></SONo>
            <InvoiceNo></InvoiceNo>
            <InvoiceItemNo>0</InvoiceItemNo>
            <DefectSerialNo></DefectSerialNo>
            <POStatus></POStatus>
            <TrackingNo></TrackingNo>
            <TrackingHyperLink></TrackingHyperLink>
            <PostGoodsIssue>N</PostGoodsIssue>
        </ROW>
        <ROW>
            <SeqNo>0002</SeqNo>
            <PartStatus>Used</PartStatus>
            <PartNo>DE26-00126B</PartNo>
            <Description>TRANS H.V;SHV-U1870D,120V,2545,2385V/3.4</Description>
            <Location></Location>
            <UsedQty>1</UsedQty>
            <RequestNo></RequestNo>
            <PONo></PONo>
            <SONo></SONo>
            <InvoiceNo>8424097053</InvoiceNo>
            <InvoiceItemNo>0</InvoiceItemNo>
            <DefectSerialNo></DefectSerialNo>
            <POStatus></POStatus>
            <TrackingNo></TrackingNo>
            <TrackingHyperLink></TrackingHyperLink>
            <PostGoodsIssue>P</PostGoodsIssue>
        </ROW>
        <ROW>
            <SeqNo>0003</SeqNo>
            <PartStatus>Used</PartStatus>
            <PartNo>DE60-20066A</PartNo>
            <Description>BOLT-FLAT;MSWR,L100,UNF1/4</Description>
            <Location></Location>
            <UsedQty>1</UsedQty>
            <RequestNo></RequestNo>
            <PONo></PONo>
            <SONo></SONo>
            <InvoiceNo>8456512557</InvoiceNo>
            <InvoiceItemNo>0</InvoiceItemNo>
            <DefectSerialNo></DefectSerialNo>
            <POStatus></POStatus>
            <TrackingNo></TrackingNo>
            <TrackingHyperLink></TrackingHyperLink>
            <PostGoodsIssue>P</PostGoodsIssue>
        </ROW>
        <ROW>
            <SeqNo>0004</SeqNo>
            <PartStatus>Used</PartStatus>
            <PartNo>DE94-03275A</PartNo>
            <Description>ASSY PANEL OUTER;MD4,BLACK,P/OUTER+ASSY-</Description>
            <Location></Location>
            <UsedQty>1</UsedQty>
            <RequestNo></RequestNo>
            <PONo></PONo>
            <SONo></SONo>
            <InvoiceNo>8456512595</InvoiceNo>
            <InvoiceItemNo>0</InvoiceItemNo>
            <DefectSerialNo></DefectSerialNo>
            <POStatus></POStatus>
            <TrackingNo></TrackingNo>
            <TrackingHyperLink></TrackingHyperLink>
            <PostGoodsIssue>P</PostGoodsIssue>
        </ROW>
        <ROW>
            <SeqNo>0005</SeqNo>
            <PartStatus>Used</PartStatus>
            <PartNo>DE96-00269A</PartNo>
            <Description>ASSY HVC;SMH7175,-,-,0.91UF HVC,-,-</Description>
            <Location></Location>
            <UsedQty>1</UsedQty>
            <RequestNo></RequestNo>
            <PONo></PONo>
            <SONo></SONo>
            <InvoiceNo>8456090576</InvoiceNo>
            <InvoiceItemNo>0</InvoiceItemNo>
            <DefectSerialNo></DefectSerialNo>
            <POStatus></POStatus>
            <TrackingNo></TrackingNo>
            <TrackingHyperLink></TrackingHyperLink>
            <PostGoodsIssue>P</PostGoodsIssue>
        </ROW>
    </Table>
    <Table>
        <Name>RecommendedPartTab</Name>
        <Column>
            <Name>PartNo</Name>
            <Type>String</Type>
        </Column>
        <Column>
            <Name>Description</Name>
            <Type>String</Type>
        </Column>
        <Column>
            <Name>Comments</Name>
            <Type>String</Type>
        </Column>
    </Table>

2 个答案:

答案 0 :(得分:0)

您可以尝试这样

import xml.etree.ElementTree as ET
tree = ET.parse('./sample2.xml')
root = tree.getroot()
for table in root.findall('Table'):
    if table.find('Name').text == 'UsedPartTab':
        for child in table.findall('Column'):
            print (child.find('Name').text)

答案 1 :(得分:0)

这里

import xml.etree.ElementTree as ET


xml = '''<rootdoc>

    <Table>
        <ROW>
            <SeqNo>0001</SeqNo>
            <PartStatus>Used</PartStatus>
            <PartNo>0M75P-21-ESGN</PartNo>
            <Description></Description>
            <Location></Location>
            <UsedQty>1</UsedQty>
            <RequestNo></RequestNo>
            <PONo></PONo>
            <SONo></SONo>
            <InvoiceNo></InvoiceNo>
            <InvoiceItemNo>0</InvoiceItemNo>
            <DefectSerialNo></DefectSerialNo>
            <POStatus></POStatus>
            <TrackingNo></TrackingNo>
            <TrackingHyperLink></TrackingHyperLink>
            <PostGoodsIssue>N</PostGoodsIssue>
        </ROW>
        <ROW>
            <SeqNo>0002</SeqNo>
            <PartStatus>Used</PartStatus>
            <PartNo>DE26-00126B</PartNo>
            <Description>TRANS H.V;SHV-U1870D,120V,2545,2385V/3.4</Description>
            <Location></Location>
            <UsedQty>1</UsedQty>
            <RequestNo></RequestNo>
            <PONo></PONo>
            <SONo></SONo>
            <InvoiceNo>8424097053</InvoiceNo>
            <InvoiceItemNo>0</InvoiceItemNo>
            <DefectSerialNo></DefectSerialNo>
            <POStatus></POStatus>
            <TrackingNo></TrackingNo>
            <TrackingHyperLink></TrackingHyperLink>
            <PostGoodsIssue>P</PostGoodsIssue>
        </ROW></Table></rootdoc>
  '''
root = ET.fromstring(xml)
fields = ['PartNo', 'UsedQty', 'InvoiceNo']
rows = root.findall('.//Table/ROW')
for row in rows:
    for field in fields:
        val = row.find('./{}'.format(field)).text
        print('{} : {}'.format(field,val))

输出

PartNo : 0M75P-21-ESGN
UsedQty : 1
InvoiceNo : None
PartNo : DE26-00126B
UsedQty : 1
InvoiceNo : 8424097053