0

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

<?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>info@novak.cz</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>

请问,我怎样才能得到数据,如:(?)

inv:invoiceSummary - typ:priceHighSum

inv:partnerIdentity - typ:name, typ:ico

inv:myIdentity - 类型:公司

inv:清算 - 类型:日期

我试过这个但不能让它工作:

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
4

2 回答 2

0

尝试使用 jsoup。一个相关的例子是解析 XML

于 2012-12-30T23:07:34.717 回答
0

这有效:

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
于 2013-01-01T12:29:33.417 回答