本文介绍了python使用ElementTree读取复杂的xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python元素树解析此xml文件:

I am trying to parse this xml file with python element tree:

<?xml version="1.0" encoding="Windows-1250"?>
<rsp:responsePack version="2.0" id="001" state="ok" note="" programVersion="9801.8 (19.5.2011)" xmlns:rsp="http://www.stormware.cz/schema/version_2/response.xsd" xmlns:rdc="http://www.stormware.cz/schema/version_2/documentresponse.xsd" xmlns:typ="http://www.stormware.cz/schema/version_2/type.xsd" xmlns:lst="http://www.stormware.cz/schema/version_2/list.xsd" xmlns:lStk="http://www.stormware.cz/schema/version_2/list_stock.xsd" xmlns:lAdb="http://www.stormware.cz/schema/version_2/list_addBook.xsd" xmlns:acu="http://www.stormware.cz/schema/version_2/accountingunit.xsd" xmlns:inv="http://www.stormware.cz/schema/version_2/invoice.xsd" xmlns:vch="http://www.stormware.cz/schema/version_2/voucher.xsd" xmlns:int="http://www.stormware.cz/schema/version_2/intDoc.xsd" xmlns:stk="http://www.stormware.cz/schema/version_2/stock.xsd" xmlns:ord="http://www.stormware.cz/schema/version_2/order.xsd" xmlns:ofr="http://www.stormware.cz/schema/version_2/offer.xsd" xmlns:enq="http://www.stormware.cz/schema/version_2/enquiry.xsd" xmlns:vyd="http://www.stormware.cz/schema/version_2/vydejka.xsd" xmlns:pri="http://www.stormware.cz/schema/version_2/prijemka.xsd" xmlns:bal="http://www.stormware.cz/schema/version_2/balance.xsd" xmlns:pre="http://www.stormware.cz/schema/version_2/prevodka.xsd" xmlns:vyr="http://www.stormware.cz/schema/version_2/vyroba.xsd" xmlns:pro="http://www.stormware.cz/schema/version_2/prodejka.xsd" xmlns:con="http://www.stormware.cz/schema/version_2/contract.xsd" xmlns:adb="http://www.stormware.cz/schema/version_2/addressbook.xsd" xmlns:prm="http://www.stormware.cz/schema/version_2/parameter.xsd" xmlns:lCon="http://www.stormware.cz/schema/version_2/list_contract.xsd" xmlns:ctg="http://www.stormware.cz/schema/version_2/category.xsd" xmlns:ipm="http://www.stormware.cz/schema/version_2/intParam.xsd">
<rsp:responsePackItem version="2.0" id="li1" state="ok">
    <lst:listInvoice version="2.0" dateTimeStamp="2011-05-27T10:47:23Z" dateValidFrom="2011-05-27" state="ok">
        <lst:invoice version="2.0">
            <inv:invoiceHeader>
                <inv:id>20</inv:id>
                <inv:invoiceType>issuedInvoice</inv:invoiceType>
                <inv:number>
                    <typ:id>26</typ:id>
                    <typ:ids>1101</typ:ids>
                    <typ:numberRequested>110100001</typ:numberRequested>
                </inv:number>
                <inv:symVar>110100001</inv:symVar>
                <inv:date>2011-01-30</inv:date>
                <inv:dateTax>2011-01-30</inv:dateTax>
                <inv:dateAccounting>2011-01-30</inv:dateAccounting>
                <inv:dateDue>2011-02-13</inv:dateDue>
                <inv:accounting>
                    <typ:id>17</typ:id>
                    <typ:ids>3Fv</typ:ids>
                </inv:accounting>
                <inv:classificationVAT>
                    <typ:id>251</typ:id>
                    <typ:ids>UD</typ:ids>
                    <typ:classificationVATType/>
                </inv:classificationVAT>
                <inv:text>Fakturujeme Vám zboží dle Vaší objednávky:  </inv:text>
                <inv:partnerIdentity>
                    <typ:id>15</typ:id>
                    <typ:address>
                        <typ:company>INTEAK spol. s r. o.</typ:company>
                        <typ:division>prodejna</typ:division>
                        <typ:name>David Jánský</typ:name>
                        <typ:city>Benešovice</typ:city>
                        <typ:street>Jiřího z Poděbrad 35</typ:street>
                        <typ:zip>463 48</typ:zip>
                        <typ:ico>85236972</typ:ico>
                        <typ:dic>CZ85236972</typ:dic>
                    </typ:address>
                    <typ:shipToAddress>
                        <typ:company/>
                        <typ:division/>
                        <typ:name/>
                        <typ:city/>
                        <typ:street/>
                    </typ:shipToAddress>
                </inv:partnerIdentity>
                <inv:myIdentity>
                    <typ:address>
                        <typ:company>Novák </typ:company>
                        <typ:surname>Novák</typ:surname>
                        <typ:name>Jan</typ:name>
                        <typ:city>Jihlava 1</typ:city>
                        <typ:street>Horní</typ:street>
                        <typ:number>15</typ:number>
                        <typ:zip>586 01</typ:zip>
                        <typ:ico>12345678</typ:ico>
                        <typ:dic>CZ12345678</typ:dic>
                        <typ:phone>569 876 542</typ:phone>
                        <typ:mobilPhone>602 852 369</typ:mobilPhone>
                        <typ:fax>564 563 216</typ:fax>
                        <typ:email>[email protected]</typ:email>
                        <typ:www>www.novak.cz</typ:www>
                    </typ:address>
                </inv:myIdentity>
                <inv:dateOrder>2011-01-22</inv:dateOrder>
                <inv:paymentType>
                    <typ:id>1</typ:id>
                    <typ:ids>příkazem</typ:ids>
                    <typ:paymentType>draft</typ:paymentType>
                </inv:paymentType>
                <inv:account>
                    <typ:id>2</typ:id>
                    <typ:ids>KB</typ:ids>
                </inv:account>
                <inv:symConst>0308</inv:symConst>
                <inv:centre>
                    <typ:id>1</typ:id>
                    <typ:ids>BRNO</typ:ids>
                </inv:centre>
                <inv:activity>
                    <typ:id>2</typ:id>
                    <typ:ids>NÁBYTEK</typ:ids>
                </inv:activity>
                <inv:liquidation>
                    <typ:date>2011-02-12</typ:date>
                    <typ:amountHome>356</typ:amountHome>
                </inv:liquidation>
            </inv:invoiceHeader>
            <inv:invoiceDetail>
                <inv:invoiceItem>
                    <inv:id>19</inv:id>
                    <inv:text>Židle Z220</inv:text>
                    <inv:quantity>2.0</inv:quantity>
                    <inv:unit>ks</inv:unit>
                    <inv:coefficient>1.0</inv:coefficient>
                    <inv:rateVAT>high</inv:rateVAT>
                    <inv:discountPercentage>0.0</inv:discountPercentage>
                    <inv:homeCurrency>
                        <typ:unitPrice>1968</typ:unitPrice>
                        <typ:price>3936</typ:price>
                        <typ:priceVAT>787.2</typ:priceVAT>
                        <typ:priceSum>4723.2</typ:priceSum>
                    </inv:homeCurrency>
                    <inv:code>Z220</inv:code>
                    <inv:guarantee>0</inv:guarantee>
                    <inv:guaranteeType>none</inv:guaranteeType>
                    <inv:stockItem>
                        <typ:store>
                            <typ:id>1</typ:id>
                            <typ:ids>ZBOŽÍ</typ:ids>
                        </typ:store>
                        <typ:stockItem>
                            <typ:id>27</typ:id>
                            <typ:ids>Z220</typ:ids>
                            <typ:PLU>650</typ:PLU>
                        </typ:stockItem>
                    </inv:stockItem>
                </inv:invoiceItem>
                <inv:invoiceItem>
                    <inv:id>20</inv:id>
                    <inv:text>Konferenční stolek chrom</inv:text>
                    <inv:quantity>1.0</inv:quantity>
                    <inv:unit>ks</inv:unit>
                    <inv:coefficient>1.0</inv:coefficient>
                    <inv:rateVAT>high</inv:rateVAT>
                    <inv:discountPercentage>0.0</inv:discountPercentage>
                    <inv:homeCurrency>
                        <typ:unitPrice>7680</typ:unitPrice>
                        <typ:price>7680</typ:price>
                        <typ:priceVAT>1536</typ:priceVAT>
                        <typ:priceSum>9216</typ:priceSum>
                    </inv:homeCurrency>
                    <inv:note>Rozměr: 120 x 60</inv:note>
                    <inv:code>Konf11</inv:code>
                    <inv:guarantee>0</inv:guarantee>
                    <inv:guaranteeType>none</inv:guaranteeType>
                    <inv:stockItem>
                        <typ:store>
                            <typ:id>1</typ:id>
                            <typ:ids>ZBOŽÍ</typ:ids>
                        </typ:store>
                        <typ:stockItem>
                            <typ:id>10</typ:id>
                            <typ:ids>Konf11</typ:ids>
                            <typ:PLU>625</typ:PLU>
                        </typ:stockItem>
                    </inv:stockItem>
                </inv:invoiceItem>
                <inv:invoiceItem>
                    <inv:id>21</inv:id>
                    <inv:text>Křeslo čalouněné 1320</inv:text>
                    <inv:quantity>4.0</inv:quantity>
                    <inv:unit>ks</inv:unit>
                    <inv:coefficient>1.0</inv:coefficient>
                    <inv:rateVAT>high</inv:rateVAT>
                    <inv:discountPercentage>0.0</inv:discountPercentage>
                    <inv:homeCurrency>
                        <typ:unitPrice>5988</typ:unitPrice>
                        <typ:price>23952</typ:price>
                        <typ:priceVAT>4790.4</typ:priceVAT>
                        <typ:priceSum>28742.4</typ:priceSum>
                    </inv:homeCurrency>
                    <inv:code>Kř1320</inv:code>
                    <inv:guarantee>0</inv:guarantee>
                    <inv:guaranteeType>none</inv:guaranteeType>
                    <inv:stockItem>
                        <typ:store>
                            <typ:id>1</typ:id>
                            <typ:ids>ZBOŽÍ</typ:ids>
                        </typ:store>
                        <typ:stockItem>
                            <typ:id>13</typ:id>
                            <typ:ids>Kř1320</typ:ids>
                            <typ:PLU>627</typ:PLU>
                        </typ:stockItem>
                    </inv:stockItem>
                </inv:invoiceItem>
            </inv:invoiceDetail>
            <inv:invoiceSummary>
                <inv:roundingDocument>up2one</inv:roundingDocument>
                <inv:roundingVAT>none</inv:roundingVAT>
                <inv:homeCurrency>
                    <typ:priceNone>0</typ:priceNone>
                    <typ:priceLow>0</typ:priceLow>
                    <typ:priceLowVAT>0</typ:priceLowVAT>
                    <typ:priceLowSum>0</typ:priceLowSum>
                    <typ:priceHigh>35568</typ:priceHigh>
                    <typ:priceHighVAT>7113.6</typ:priceHighVAT>
                    <typ:priceHighSum>42681.6</typ:priceHighSum>
                    <typ:round>
                        <typ:priceRound>0.4</typ:priceRound>
                    </typ:round>
                </inv:homeCurrency>
            </inv:invoiceSummary>
        </lst:invoice>
    </lst:listInvoice>
</rsp:responsePackItem>
</rsp:responsePack>

请,我如何获取数据,例如:(?)

Please, how can I get data such as: (?)

inv:invoiceSummary-typ:priceHighSum

inv:invoiceSummary - typ:priceHighSum

inv:partnerIdentity-typ:name,typ:ico

inv:partnerIdentity - typ:name, typ:ico

inv:myIdentity-类型:公司

inv:myIdentity - typ:company

inv:清算-类型:日期

inv:liquidation - typ:date

I尝试过此方法,但无法使其正常工作:

I tried this but can't get it working:

import xml.etree.ElementTree as ET
tree = ET.parse('temp_xml2.xml')
root = tree.getroot()


for listInvoice in root.findall('listInvoice'):
    invoiceHeader = listInvoice.find('invoiceHeader').text
    print invoiceHeader


推荐答案

这有效:

for listInvoice in root.findall('.//{http://www.stormware.cz/schema/version_2/invoice.xsd}invoiceHeader'):
    invoiceHeader = listInvoice.find('.//{http://www.stormware.cz/schema/version_2/invoice.xsd}id').text
    print invoiceHeader

这篇关于python使用ElementTree读取复杂的xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 22:59