I need to file from

<?xml version="1.0" encoding="UTF-8"?> <messages> <message> <title>Заголовок</title> <text>Текст сообщения</text> </message> </messages> 

it happened

 object.message[0].title 

I can not find a ready-made solution.
I would be grateful if you tell me.

    2 answers 2

    python-simplexml

    Overview

    If I started working in Python Coming from PHP I had SimpleXML that worked perfectly fine for 99% of the XML parsing I had to do. Wrapping around the ElementTree.

    Usage

     from simplexml import parse xml = parse("person.xml") print xml.name.first print xml.name.last print xml.name["origin"] 
    • Simplexml is convenient, but if it is relevant: 1) its development froze in 2005 and compatibility with python 3.x is not expected yet; 2) lxml by tests shows the best speed of parsing, it will be noticeable on files of several MB. - vostbur
    • There's also the simplest wrapper of getters , what else is there to develop :) - Ilya Pirogov
    • I agree, nothing special. Rather, it is necessary to develop in the direction of increasing speed. Here is a comparison of LXML with xml.etree.cElementTree (xml.etree.ElementTree) blog.behnel.de/index.php?p=235 - vostbur
     from lxml import etree tree = etree.parse('example.xml') raws = tree.xpath('//message') for raw in raws: print raw.xpath('title')[0].text 

    if there is only one item

     raws = tree.xpath('//message/title') print raws[0].text