[Chicago] xml to py object

Massimo Di Pierro mdipierro at cs.depaul.edu
Fri May 1 16:42:49 CEST 2009


This is a quick solution with no dependencies:

import re


class Parser:
     class S(dict):
         def __setattr__(self,k,v): self[k]=v
         def __getattr__(self,k): return self[k]
     r1=re.compile('^\s*\<(?P<tag>\w+)(?P<attr>.*?)>')
     r2=re.compile('(?P<name>\w+)\s*=\s*"(?P<value>([^\\\\]|\\\\")*?)"')
     def __init__(self,text): (self.text,self.value)=(text,Parser.S())
     def parse(self,text=None,value=None):
         if not text: (text,value)=(self.text,self.value)
         done=False
         while True:
             m=self.r1.search(text)
             if not m:
                 if not done: value.value=text
                 break
             tag=m.group('tag')
             k=text.find('</%s>'%tag)
             if k<0:
                 if not done: value.value=text
                 break
             nested = value[tag] = Parser.S()
             nested.attr=Parser.S()
             for item in self.r2.finditer(m.group('attr')):
                 nested.attr[item.group('name')]=item.group('value')
             self.parse(text[m.end():k],nested)
             (text, done) = (text[k+3+len(tag):],True)
         return value


x=Parser('<hello n="2"><a n="4" m="6">in a</a><b>in b</b></ 
hello>').parse()
print x
print x.hello
print x.hello.attr.n
print x.hello.a.value
print x.hello.a.attr.n
print x.hello.a.attr.m
print x.hello.b.value



On May 1, 2009, at 8:34 AM, Lukasz Szybalski wrote:

> Hello,
> I'm trying to convert an xml to python like object where:
>
>      <ClientApp>
>            <Org>my company</Org>
>             <Name>Full name of the comapny</Name>
>            <Version>1.02 (4.02)</Version>
>        </ClientApp>
>
> would correspond to
>
> class someobject(object):........
>
> and would be accessible via something like:
>
> print x.ClientApp.Org
> print x.ClientApp.Name
> print x.ClientApp.Version
>
> Not sure how things like this would be implemented:
>
>  <Vehicle id="Veh_1" LocationRef="Location_1" DriverRef="Driver_1"/>
>
> Is there a module/library that is able convert the xml into  
> something like this?
>
> Thanks,
> Lucas
>
>
> -- 
> How to create python package?
> http://lucasmanual.com/mywiki/PythonPaste
> DataHub - create a package that gets, parses, loads, visualizes data
> http://lucasmanual.com/mywiki/DataHub
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago



More information about the Chicago mailing list