Y
Y
Yngvie2011-10-17 16:16:15
Python
Yngvie, 2011-10-17 16:16:15

XML Mapper for Python

Good afternoon,

Recently there was a problem with XML parsing, and I wanted it to be convenient.

I would like to be able to use it like this:

class RaceMapper(XMLMapper):
    """Example usage
    """
    time = XMLAttrib(attrib='time')
    date = XMLAttrib(attrib='date')
    title = XMLAttrib(xpath='Title')
    eligibility = XMLAttrib(xpath='Eligibility', attrib='type')


if __name__ == '__main__:
    tree = etree.fromstring(open(xml_file_path).read())
    race = RaceMapper(tree.find('.//Race'))
    print race.date, race.time, race.title, race.eligibility


This code can be fed a piece of XML type:
<Race id="187505" date="20080414" time="1410+0100" raceType="Hurdle" >
   <Title>Parklands Golf For All Conditionals' Novices' Handicap Hdl</Title>
   <Eligibility type="4YO plus"/>
   <Distance units="yards" value="5280" text="3m 0f 0y"/>
</Race>


And it will be parsed.
I threw a blank for such functionality

. But I had a question, is there really no ready-made easy solution for such purposes?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
dborovikov, 2011-11-07
@Yngvie

Similar to pypi.python.org/pypi/dexml/

A
Ano, 2011-10-17
@Ano

Well there is lxml.de/objectify.html

M
MichaelXIII, 2011-10-19
@MichaelXIII

Why reinvent XPath/XSLT if they already exist?
Or did I not understand something?

F
FINTER, 2011-10-30
@FINTER

Take a closer look at PyQuery. It's something like jQuery.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question