2 Replies - 5841 Views - Last Post: 12 July 2012 - 04:24 PM Rate Topic: -----

#1 Acidogenic  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 07-July 12

Parsing XML processing instructions in python

Posted 07 July 2012 - 12:51 PM

I am attempting to parse a file that contains a list of these:

        <card number="1" price="0" name="Trade Secrets"/>


and I would like to pull out the number and name strings. I am currently using SAX in the document for another parser (xml formatted so I can use characters()). Can I use xml.sax.processingInstruction() or do I need to do something else? And how do I implement it? The pydocs are not helping me any right now.

Is This A Good Question/Topic? 0
  • +

Replies To: Parsing XML processing instructions in python

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6081
  • View blog
  • Posts: 23,556
  • Joined: 23-August 08

Re: Parsing XML processing instructions in python

Posted 08 July 2012 - 04:25 AM

price and name are attributes of the card node; processing instructions are not something you're interested in here. Think you're looking for this.
Was This Post Helpful? 1
  • +
  • -

#3 Acidogenic  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 07-July 12

Re: Parsing XML processing instructions in python

Posted 12 July 2012 - 04:24 PM

Thanks Jack, I finally did find what I was looking for. I have no xml background, so pointing me to the attributes helped a lot. What I needed here was this piece of code in my parser:

def startElement(self, name, attrs):
      if(name=='card'):
            cardname = attrs.get('name')
            cardnumber= attrs.get('number')
 


That found the needed information and returned it.Thanks for starting me in the right direction!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1