xml.dom.minidom question

G

Geiregat Jonas

I'm using xml.dom.minidom, I get some data by using
obj.getElementsByTagName("name")[0] then I have an object, but how can I
get the data between the tags ? I could do .toxml() and then strip it
away. But is there a better way maybe a function made for it ?
Also is it possible using the xml.dom.minidom module to create modify an
xml file ?
 
G

Greg Krohn

Geiregat Jonas said:
I'm using xml.dom.minidom, I get some data by using
obj.getElementsByTagName("name")[0] then I have an object, but how can I
get the data between the tags ? I could do .toxml() and then strip it
away. But is there a better way maybe a function made for it ?
Also is it possible using the xml.dom.minidom module to create modify an
xml file ?

Given <name>Greg</name>, 'Greg' is considered an element inside the 'name'
element. So, all you have to do is go one level of elements deeper.

myname = obj.getElementsByTagName("name")[0].childNodes[0].data


greg
 
J

John J. Lee

Greg Krohn said:
Geiregat Jonas said:
obj.getElementsByTagName("name")[0] then I have an object, but how can I
get the data between the tags ? I could do .toxml() and then strip it
[...]
Given <name>Greg</name>, 'Greg' is considered an element inside the 'name'
element. So, all you have to do is go one level of elements deeper.

myname = obj.getElementsByTagName("name")[0].childNodes[0].data

Might want to read up about normalization, too.


John
 
A

Alan Kennedy

Peter said:

Actually, Peter, that link has nothing to do with the normalization of
text nodes in a DOM, which was, I think, the "normalization" in
question.

The link you have provided gives detail on "attribute value
normalization", which is the normalization of whitespace in attribute
values, not DOM text nodes.

Of course, the normalization of test nodes in a DOM is a not a part of
the XML spec, it is a part of the DOM spec.

http://www.w3.org/TR/DOM-Level-2-Core/core.html

Check out the Node.normalize() method.
 
G

Geoff Gerrietts

Quoting Geiregat Jonas ([email protected]):
But how ???

from xml.dom.minidom import parse

dom = parse(my_xml_file)

dom = makeChangesToMyDomTree(dom)

f = open(my_xml_file)
f.write(dom.toxml())

You should verify that this works properly but it should. I remember
at one point observing a problem with a document that declared a DTD,
where the DTD was not preserved and invalid XML was dumped. I don't
remember if minidom was the parser I was using, though, and I don't
have the time to test right now.

--G.
 
U

Uche Ogbuji

Greg Krohn said:
Geiregat Jonas said:
I'm using xml.dom.minidom, I get some data by using
obj.getElementsByTagName("name")[0] then I have an object, but how can I
get the data between the tags ? I could do .toxml() and then strip it
away. But is there a better way maybe a function made for it ?
Also is it possible using the xml.dom.minidom module to create modify an
xml file ?

Given <name>Greg</name>, 'Greg' is considered an element inside the 'name'
element. So, all you have to do is go one level of elements deeper.

You mean it's considered a text node inside of the 'name' element
node.

I'll point out that it might actually be more than one text node.
Only safe way is to use the normalize() method.

Better yet, why torture yourself with DOM API, especially when all you
need is to extract data from XML files?

My recommendations would be to use Anobind, Elementtree,
gnosis.xml.objectify or 4XPath (an advanage of XPath is that at least
the extraction expression is ross-language):

Anobind:

http://uche.ogbuji.net/tech/4Suite/anobind/
http://www.xml.com/pub/a/2003/08/13/py-xml.html

Elementtree:

http://effbot.org/zone/element-index.htm
http://www.xml.com/pub/a/2003/02/12/py-xml.html

gnosis.xml.objectify:

http://www.xml.com/pub/a/2003/07/02/py-xml.html
http://www-106.ibm.com/developerworks/xml/library/x-matters11.html

4XPath:

http://uche.ogbuji.net/tech/akara/nodes/2003-01-01/basic-xpath
http://www.xml.com/pub/a/2002/10/16/py-xml.html

Good luck.

--Uche
http://uche.ogbuji.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top