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. ;) - import xml
-
from xml.dom.minidom import parseString
-
-
def getChild(self, name):
-
print "Getting child"
-
return self.getElementsByTagName(name)[0]
-
-
xml.dom.minidom.Node.__getattr__ = getChild
-
-
xmlDoc = parseString('''<?xml version="1.0" encoding="ISO-8859-1"?>
-
<CATALOG>
-
<cd>
-
<title>Empire Burlesque</title>
-
<ARTIST>Bob Dylan</ARTIST>
-
<COUNTRY>USA</COUNTRY>
-
<COMPANY>Columbia</COMPANY>
-
<PRICE>10.90</PRICE>
-
<YEAR>1985</YEAR>
-
</cd>
-
<cd>
-
<title>Hide your heart</title>
-
<ARTIST>Bonnie Tylor</ARTIST>
-
<COUNTRY>UK</COUNTRY>
-
<COMPANY>CBS Records</COMPANY>
-
<PRICE>9.90</PRICE>
-
<YEAR>1988</YEAR>
-
</cd>
-
</CATALOG>''')
-
-
xmlRoot = xmlDoc.childNodes[0]
-
print xmlRoot.__getattr__
-
print xmlRoot.cd.title.childNodes[0].nodeValue
outputs: - <bound method Element.getChild of <DOM Element: CATALOG at 0x1c50850>>
-
File "C:\Users\Tuomas\Projektit\wrestlevania\ezXML.py", line 31, in <module>
-
print xmlRoot.cd.title.childNodes[0].nodeValue
-
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?
13 2133
If you're trying to call __getattr__ you need ().
If you're trying to call __getattr__ you need ().
Not trying to call it - I'm overriding it.
Not trying to call it - I'm overriding it.
No I know that... but you still need the (name) arguments that getChild needs
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.
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?
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. ;) - import xml
-
from xml.dom.minidom import parseString
-
-
def getChild(self, name):
-
print "Getting child"
-
return self.getElementsByTagName(name)[0]
-
-
xml.dom.minidom.Node.__getattr__ = getChild
-
-
xmlDoc = parseString('''<?xml version="1.0" encoding="ISO-8859-1"?>
-
<CATALOG>
-
<cd>
-
<title>Empire Burlesque</title>
-
<ARTIST>Bob Dylan</ARTIST>
-
<COUNTRY>USA</COUNTRY>
-
<COMPANY>Columbia</COMPANY>
-
<PRICE>10.90</PRICE>
-
<YEAR>1985</YEAR>
-
</cd>
-
<cd>
-
<title>Hide your heart</title>
-
<ARTIST>Bonnie Tylor</ARTIST>
-
<COUNTRY>UK</COUNTRY>
-
<COMPANY>CBS Records</COMPANY>
-
<PRICE>9.90</PRICE>
-
<YEAR>1988</YEAR>
-
</cd>
-
</CATALOG>''')
-
-
xmlRoot = xmlDoc.childNodes[0]
-
print xmlRoot.__getattr__
-
print xmlRoot.cd.title.childNodes[0].nodeValue
outputs: - <bound method Element.getChild of <DOM Element: CATALOG at 0x1c50850>>
-
File "C:\Users\Tuomas\Projektit\wrestlevania\ezXML.py", line 31, in <module>
-
print xmlRoot.cd.title.childNodes[0].nodeValue
-
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: - def getChild(self, name):
-
print "Getting child"
-
return self.getElementsByTagName(name)
Modify the calls: - xmlRoot = xmlDoc.childNodes[0]
-
print xmlRoot.__getattr__('cd')
-
print xmlRoot.__getattr__('cd')[0].__getattr__('title')[0].firstChild.nodeValue
-
print xmlRoot.__getattr__('ARTIST')[0].firstChild.nodeValue
Output: - >>> Getting child
-
[<DOM Element: cd at 0x100d4b8>, <DOM Element: cd at 0x100d788>]
-
Getting child
-
Getting child
-
Empire Burlesque
-
Getting child
-
Bob Dylan
-
>>>
Modify the calls: -
print xmlRoot.__getattr__('cd')
-
That's what I was trying to say by using the (name) argument
bvdet 2,851
Recognized Expert Moderator Specialist
That's what I was trying to say by using the (name) argument
And you were correct!
- xmlRoot = xmlDoc.childNodes[0]
-
print xmlRoot.__getattr__('cd')
-
print xmlRoot.__getattr__('cd')[0].__getattr__('title')[0].firstChild.nodeValue
-
print xmlRoot.__getattr__('ARTIST')[0].firstChild.nodeValue
Output: - >>> Getting child
-
[<DOM Element: cd at 0x100d4b8>, <DOM Element: cd at 0x100d788>]
-
Getting child
-
Getting child
-
Empire Burlesque
-
Getting child
-
Bob Dylan
-
>>>
In my calls I needed to modify it a little bit... -
>>> xmlRoot.__getattr__('cd').__getattr__('title').firstChild.nodeValue
-
Getting child
-
Getting child
-
u'Empire Burlesque'
-
>>> xmlRoot.__getattr__('ARTIST').firstChild.nodeValue
-
Getting child
-
u'Bob Dylan'
-
>>>
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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>
|
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
|
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?
|
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
|
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:
| |
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
|
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...
|
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')
|
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
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |