473,657 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Minidom.Node doesn't call __getattr__

5 New Member
I'm using minidom to parse XML and to simplify accessing child nodes, I'm trying to implement __getattr__ for the Node class. Yes, I know what most of you will think of that; I'll just have to be careful, won't I. ;)

Expand|Select|Wrap|Line Numbers
  1. import xml
  2. from xml.dom.minidom import parseString
  3.  
  4. def getChild(self, name):
  5.     print "Getting child"
  6.     return self.getElementsByTagName(name)[0]
  7.  
  8. xml.dom.minidom.Node.__getattr__ = getChild
  9.  
  10. xmlDoc = parseString('''<?xml version="1.0" encoding="ISO-8859-1"?>
  11. <CATALOG>
  12.   <cd>
  13.     <title>Empire Burlesque</title>
  14.     <ARTIST>Bob Dylan</ARTIST>
  15.     <COUNTRY>USA</COUNTRY>
  16.     <COMPANY>Columbia</COMPANY>
  17.     <PRICE>10.90</PRICE>
  18.     <YEAR>1985</YEAR>
  19.   </cd>
  20.   <cd>
  21.     <title>Hide your heart</title>
  22.     <ARTIST>Bonnie Tylor</ARTIST>
  23.     <COUNTRY>UK</COUNTRY>
  24.     <COMPANY>CBS Records</COMPANY>
  25.     <PRICE>9.90</PRICE>
  26.     <YEAR>1988</YEAR>
  27.   </cd>
  28. </CATALOG>''')
  29.  
  30. xmlRoot = xmlDoc.childNodes[0]
  31. print xmlRoot.__getattr__
  32. print xmlRoot.cd.title.childNodes[0].nodeValue
outputs:
Expand|Select|Wrap|Line Numbers
  1. <bound method Element.getChild of <DOM Element: CATALOG at 0x1c50850>>
  2.   File "C:\Users\Tuomas\Projektit\wrestlevania\ezXML.py", line 31, in <module>
  3.     print xmlRoot.cd.title.childNodes[0].nodeValue
  4. AttributeError: Element instance has no attribute 'cd'
As far as I can tell, everything works fine, except __getattr__ never gets called. Can anyone help me understand why that is or how to work around it?
Mar 7 '08 #1
13 2133
jlm699
314 Contributor
If you're trying to call __getattr__ you need ().
Mar 7 '08 #2
ArseAssassin
5 New Member
If you're trying to call __getattr__ you need ().
Not trying to call it - I'm overriding it.
Mar 7 '08 #3
jlm699
314 Contributor
Not trying to call it - I'm overriding it.
No I know that... but you still need the (name) arguments that getChild needs
Mar 7 '08 #4
ArseAssassin
5 New Member
No I know that... but you still need the (name) arguments that getChild needs
I'm not sure what you're getting at. getChild is just an implementation of the special method __getattr__, which should be called when I'm trying to access attributes that aren't defined.
Mar 7 '08 #5
jlm699
314 Contributor
I'm not sure what you're getting at. getChild is just an implementation of the special method __getattr__, which should be called when I'm trying to access attributes that aren't defined.
But aren't you over-riding __getattr__ with getChild ? The way that your code is setup, instead of __getattr__ being called, it calls for getChild... I don't know, maybe I'm misunderstandin g what you're trying to accomplish in this code... Where does the .cd. method come from since it's not an attribute of the class that you are trying to call it from?
Mar 7 '08 #6
bvdet
2,851 Recognized Expert Moderator Specialist
I'm using minidom to parse XML and to simplify accessing child nodes, I'm trying to implement __getattr__ for the Node class. Yes, I know what most of you will think of that; I'll just have to be careful, won't I. ;)

Expand|Select|Wrap|Line Numbers
  1. import xml
  2. from xml.dom.minidom import parseString
  3.  
  4. def getChild(self, name):
  5.     print "Getting child"
  6.     return self.getElementsByTagName(name)[0]
  7.  
  8. xml.dom.minidom.Node.__getattr__ = getChild
  9.  
  10. xmlDoc = parseString('''<?xml version="1.0" encoding="ISO-8859-1"?>
  11. <CATALOG>
  12.   <cd>
  13.     <title>Empire Burlesque</title>
  14.     <ARTIST>Bob Dylan</ARTIST>
  15.     <COUNTRY>USA</COUNTRY>
  16.     <COMPANY>Columbia</COMPANY>
  17.     <PRICE>10.90</PRICE>
  18.     <YEAR>1985</YEAR>
  19.   </cd>
  20.   <cd>
  21.     <title>Hide your heart</title>
  22.     <ARTIST>Bonnie Tylor</ARTIST>
  23.     <COUNTRY>UK</COUNTRY>
  24.     <COMPANY>CBS Records</COMPANY>
  25.     <PRICE>9.90</PRICE>
  26.     <YEAR>1988</YEAR>
  27.   </cd>
  28. </CATALOG>''')
  29.  
  30. xmlRoot = xmlDoc.childNodes[0]
  31. print xmlRoot.__getattr__
  32. print xmlRoot.cd.title.childNodes[0].nodeValue
outputs:
Expand|Select|Wrap|Line Numbers
  1. <bound method Element.getChild of <DOM Element: CATALOG at 0x1c50850>>
  2.   File "C:\Users\Tuomas\Projektit\wrestlevania\ezXML.py", line 31, in <module>
  3.     print xmlRoot.cd.title.childNodes[0].nodeValue
  4. AttributeError: Element instance has no attribute 'cd'
As far as I can tell, everything works fine, except __getattr__ never gets called. Can anyone help me understand why that is or how to work around it?
Modify getChild() slightly:
Expand|Select|Wrap|Line Numbers
  1. def getChild(self, name):
  2.     print "Getting child"
  3.     return self.getElementsByTagName(name)
Modify the calls:
Expand|Select|Wrap|Line Numbers
  1. xmlRoot = xmlDoc.childNodes[0]
  2. print xmlRoot.__getattr__('cd')
  3. print xmlRoot.__getattr__('cd')[0].__getattr__('title')[0].firstChild.nodeValue
  4. print xmlRoot.__getattr__('ARTIST')[0].firstChild.nodeValue
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> Getting child
  2. [<DOM Element: cd at 0x100d4b8>, <DOM Element: cd at 0x100d788>]
  3. Getting child
  4. Getting child
  5. Empire Burlesque
  6. Getting child
  7. Bob Dylan
  8. >>> 
Mar 7 '08 #7
jlm699
314 Contributor
Modify the calls:
Expand|Select|Wrap|Line Numbers
  1. print xmlRoot.__getattr__('cd')
  2.  
That's what I was trying to say by using the (name) argument
Mar 7 '08 #8
bvdet
2,851 Recognized Expert Moderator Specialist
That's what I was trying to say by using the (name) argument
And you were correct!
Mar 7 '08 #9
jlm699
314 Contributor
Expand|Select|Wrap|Line Numbers
  1. xmlRoot = xmlDoc.childNodes[0]
  2. print xmlRoot.__getattr__('cd')
  3. print xmlRoot.__getattr__('cd')[0].__getattr__('title')[0].firstChild.nodeValue
  4. print xmlRoot.__getattr__('ARTIST')[0].firstChild.nodeValue
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> Getting child
  2. [<DOM Element: cd at 0x100d4b8>, <DOM Element: cd at 0x100d788>]
  3. Getting child
  4. Getting child
  5. Empire Burlesque
  6. Getting child
  7. Bob Dylan
  8. >>> 
In my calls I needed to modify it a little bit...
Expand|Select|Wrap|Line Numbers
  1. >>> xmlRoot.__getattr__('cd').__getattr__('title').firstChild.nodeValue
  2. Getting child
  3. Getting child
  4. u'Empire Burlesque'
  5. >>> xmlRoot.__getattr__('ARTIST').firstChild.nodeValue
  6. Getting child
  7. u'Bob Dylan'
  8. >>> 
  9.  
Mar 7 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
7505
by: xtian | last post by:
Hi - I'm doing some data conversion with minidom (turning a csv file into a specific xml format), and I've hit a couple of small problems. 1: The output format has a header with some xml that looks something like this: <item xmlns="" xmlns:thing="http://www.blah.com"> <thing:child name="smith"/> </item>
5
2163
by: Magnus Lie Hetland | last post by:
Is it possible to build a minidom tree bottom-up without access to a document node? It seems that simply instantiating the various node classes doesn't quite do the trick... -- Magnus Lie Hetland "In this house we obey the laws of http://hetland.org thermodynamics!" Homer Simpson
0
1443
by: Oliver Walczak | last post by:
Can i call normalize to the dom root node so that all adjacent child text nodes attached to one of the element nodes are joined? -----Ursprüngliche Nachricht----- Von: python-list-bounces+oliver.walczak=momatec.de@python.org Im Auftrag von Fredrik Lundh Gesendet: Freitag, 28. November 2003 12:14 An: python-list@python.org Betreff: Re: Buffer restriction in dom.minidom?
4
5310
by: Skip Montanaro | last post by:
I'm getting somewhat painfully acquainted with xml.dom.minidom. What is the relationship between its documentElement attribute and its childNodes list? I thought XML documents consisted of a single, possibly compound, node. Why is a list of childNodes needed? Thx, Skip
4
2862
by: Derek Basch | last post by:
Hello All, I ran into a problem while dynamically constructing XHTML documents using minidom. If you create a script tag such as: script_node_0 = self.doc.createElement("script") script_node_0.setAttribute("type", "text/javascript") script_node_0.setAttribute("src", "../test.js") minidom renders it as:
4
6059
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3 script that grabs some web pages from the web, regex parse the data and stores it localy to xml file for further use.. at first i had no problem using python minidom and everything concerning
1
1633
by: Dean Card | last post by:
I am using minidom to parse a 20,000 line XML file. I have a few instances where the number of child nodes of a particular node can be variable in number. To access them I am doing something like the following... xmldoc = minidom.parseString(r) results = xmldoc.childNodes for myNode in results.childNodes.childNodes: do Stuff with myNode...
1
1968
by: Maksim Kasimov | last post by:
Hi, i'm faced with such a problem when i use xml.dom.minidom: to append all child nodes from "doc" in "_requ" to "doc" in "_resp", i do the following: _requ = minidom.parseString("<resp><doc><one>One</one><two>Two</two></doc></resp>") _resp = minidom.parseString("<resp><doc/></resp>") iSourseTag = _requ.getElementsByTagName('doc') iTargetTag = _resp.getElementsByTagName('doc')
0
1511
by: Gary | last post by:
Howdy I ran into a difference between Python on Windows XP and Linux Fedora 6. Writing a dom to xml with minidom works on Linux. It gives an error on XP if there is an empty namespace. The problem was handled in CVS a while ago. http://mail.python.org/pipermail/xml-sig/2003-October/009904.html
0
8850
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6178
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.