简体   繁体   中英

How to read xml and get text from an element using lxml

I'm a little bit confused with getting information from xml

My xml

<?xml version="1.0" encoding="UTF-8"?>
<AirShoppingRS Version="16.2" xsi:schemaLocation="http://www.iata.org/IATA/EDIST AirShoppingRS.xsd"
    xmlns="http://www.iata.org/IATA/EDIST"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Document/>
    <Success/>
    <ShoppingResponseID>
        <ResponseID>2017-10-04T14:35:25.243504</ResponseID>
    </ShoppingResponseID>
    <OffersGroup>
        <AirlineOffers>
            <TotalOfferQuantity>297</TotalOfferQuantity>
            <Owner>SU</Owner>
            <AirlineOffer>
                <OfferID Owner="SU">OFFER5</OfferID>
                <TotalPrice>
                    <SimpleCurrencyPrice Code="RUB">36229</SimpleCurrencyPrice>
                </TotalPrice>
                <PricedOffer>
                    <OfferPrice OfferItemID="5">
                        <RequestedDate>
                            <PriceDetail>
                                <TotalAmount>
                                    <SimpleCurrencyPrice>36229</SimpleCurrencyPrice>
                                </TotalAmount>
                                <BaseAmount>33000</BaseAmount>
                                <Taxes>
                                    <Total Code="RUB">3229</Total>
                                </Taxes>
                            </PriceDetail>
                        </RequestedDate>
                        <FareDetail>
                            <FareComponent>
                                <SegmentReference>SEG_SVOLED_1</SegmentReference>
                                <FareBasis>
                                    <FareBasisCode>
                                        <Code>DFOR</Code>
                                    </FareBasisCode>
                                </FareBasis>
                            </FareComponent>
                        </FareDetail>
                    </OfferPrice>
                </PricedOffer>
            </AirlineOffer>
        </AirlineOffers>
    </OffersGroup>
</AirShoppingRS>

How can I read it, using lxml library. I try this one root = etree.fromstring(xml.content) and then I tried airline_offers = root.findall("AirlineOffer") , but get nothing. Guess, I'm doing something wrong. Where do I make mistake? How can I get an element and then text or attribute from it?

UPDATE: airline_offers = root.findall(".//AirlineOffer") returns nothing too

A default namespace ( http://www.iata.org/IATA/EDIST ) is declared on the root element. Here is one way to make it work:

airline_offers = root.findall(".//{http://www.iata.org/IATA/EDIST}AirlineOffer")

It is also possible to use a wildcard:

airline_offers = root.findall(".//{*}AirlineOffer")

An alternative is to define a prefix:

NS = {"edist": "http://www.iata.org/IATA/EDIST"}
airline_offers = root.findall(".//edist:AirlineOffer", namespaces=NS)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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