xml.dom.minidom nodeValue returns none

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iamfletch
    New Member
    • Jan 2010
    • 18

    xml.dom.minidom nodeValue returns none

    What causes nodeValue to be None?
    Im trying to connect to facebook, my lack xml experiance with python is letting me down. Can anyone explain the following?

    Code:
    def getReply(self, params):
      req = urllib.urlopen(facebook.getURL(params))
      tokenXML = req.read()
      req.close()
      print 'Raw data: ', tokenXML
      return tokenXML
    
    tokenXML = parseString(facebook.getReply(params))
    token = tokenXML.getElementsByTagName('auth_createToken_response')[0]
    print 'toString: ', token
    print 'nodeValue: ', token.nodeValue
    Gives the following:
    Code:
    C:\Users\simon\Documents\Flash\air\fb++\server>server.py
    Raw data:  <?xml version="1.0" encoding="UTF-8"?>
    <auth_createToken_response xmlns="http://api.facebook.com/1.0/" xmlns:
    xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="ht
    tp://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd">9
    ea0d6592e0f761255e549fbd180443a</auth_createToken_response>
    
    toString:  <DOM Element: auth_createToken_response at 0x2335328>
    nodeValue:  None
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    parseString method getElementsByTa gName returns a list. The string held between the open and closing tags is a child node that is accessed with element method childNodes. You would access the text between the tags similar to this:
    Code:
    from xml.dom.minidom import parse, parseString
    
    xmlStr = '''<?xml version="1.0" encoding="UTF-8"?>
    <auth_createToken_response
     xmlns="http://api.facebook.com/1.0/"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://api.facebook.com/1.0/facebook.xsd">
     9ea0d6592e0f761255e549fbd180443a
    </auth_createToken_response>'''
     
    xmlDoc = parseString(xmlStr)
    for elem in xmlDoc.getElementsByTagName('auth_createToken_response'):
        for child in elem.childNodes:
            print repr(child.nodeValue.strip())
    I formatted the XML string for readability.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      You can also get the desired result in this case using method firstChild which returns the first child element or None:
      Code:
      xmlDoc = parseString(xmlStr)
      for elem in xmlDoc.getElementsByTagName('auth_createToken_response'):
          child = elem.firstChild
          if child:
              print repr(child.nodeValue.strip())

      Comment

      • iamfletch
        New Member
        • Jan 2010
        • 18

        #4
        thanks, is it me or does it make no sense to find it by tagname then have to access it by using childNodes/firstChild.

        Thanks for the fast response.
        simon

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          A tag can encompass any number of nested elements of different types. A text element (node.nodeType == 3) is just one type of node. When you look at it this way, it makes some sense. Take a look at this XML DOM tutorial.

          Comment

          • iamfletch
            New Member
            • Jan 2010
            • 18

            #6
            I get ya, makes sense sort of.

            Comment

            Working...