pure python based XML parsing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • linksterman
    New Member
    • Jul 2007
    • 38

    pure python based XML parsing

    I am developing a program for the PSP, and so far the methods for processing XML's depend on dll's, so i cannot use them as they are not compiled for use with the PlayStation Portable, bout everything i see depends on expat which needs window dll's to work

    so is there a "pure" python library? one where EVERYTHING needed for processing XML is contained in a .py file?
  • Subsciber123
    New Member
    • Nov 2006
    • 87

    #2
    You should probably look into pypy. That is a python implementation written in python. You probably won't want to use the interpreter because it is incredibly slow, but you can use the modules, which have been converted to python instead of being written in C.

    Comment

    • linksterman
      New Member
      • Jul 2007
      • 38

      #3
      EDIT:
      nvr mind, that was easy:
      Code:
      class XMLParsers:
          def __init__(self, inputString):
              self.xml = inputString
              self.returne = []
              self.toplevel = 0
          def output(self, inputl):
              wow = []
              returnme = {}
              while inputl.find("</") > -1:
                  key = inputl.split("</")[len(inputl.split("</"))-1].split(">")[0]
                  m = inputl.split("</"+key+'>')[0].split("<"+key+">")[1]
                  if (m.find("</")>-1):
                      if len(m.split("<",1)[0]) > 0:
                          returnme[key]=[m.split("<",1)[0],self.output("<"+m+">")]
                      else:
                          returnme[key]=[self.output("<"+m+">")]
                  else:
                      returnme[key]=m
                  inputl = "<"+inputl.replace("<"+key+">"+m+"</"+key+">","").strip("<>")+">"
              return returnme
                  
          def retn(self):
              return self.output(self.xml)
      That was pretty cool making that
      only problem so far is it doesnt take attributes, but it does process XML quite nicely, goes down as far as needed:
      Code:
      >>> XMLParsers("<p>lol<m>ds</m><i>p</i></p><l>my</l><oiu>wow xml parsing!</oiu>").retn()
      {'p': ['lol', {'i': 'p', 'm': 'ds'}], 'oiu': 'wow xml parsing!', 'l': 'my'}
      i just need to code for multiple instances of the same thing, and then i should be good.

      Comment

      • linksterman
        New Member
        • Jul 2007
        • 38

        #4
        ok, so i think i got this down...


        input:
        Code:
        >>> XMLParsers('<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0"><channel><title>RSS Example</title><description>This is an example of an RSS feed</description><link>http://www.domain.com/link.htm</link><lastBuildDate>Mon, 28 Aug 2006 11:12:55 -0400 </lastBuildDate><pubDate>Tue, 29 Aug 2006 09:00:00 -0400</pubDate><item><title>Item Example</title><description>This is an example of an Item</description><link>http://www.domain.com/link.htm</link><guid isPermaLink="false"> 1102345</guid><pubDate>Tue, 29 Aug 2006 09:00:00 -0400</pubDate></item></channel></rss>').retn()
        output:
        Code:
        {'rss': [[{'channel': [[{'lastBuildDate': ['Mon, 28 Aug 2006 11:12:55 -0400 '], 'description': ['This is an example of an RSS feed'], 'pubDate': ['Tue, 29 Aug 2006 09:00:00 -0400'], 'title': ['RSS Example'], 'item': [[{'guid': [' 1102345'], 'link': ['http://www.domain.com/link.htm'], 'description': ['This is an example of an Item'], 'pubDate': ['Tue, 29 Aug 2006 09:00:00 -0400'], 'title': ['Item Example']}]], 'link': ['http://www.domain.com/link.htm']}]]}]]}

        so does the output make sense to everybody?

        here is if it has multiple instances, and it handles attributes now:
        input:
        Code:
        XMLParsers("<p>first p</p><p>second p<m>ds</m><i>p</i></p><p>Third P</p><l>my</l><oiu o=moi bot>wow xml parsing!</oiu>").retn()
        output:

        Code:
        {'p': ['first p', ['second p', {'i': ['p'], 'm': ['ds']}], 'Third P'], 'oiu': ['wow xml parsing!'], 'l': ['my']}

        Comment

        Working...