473,221 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

How do I get the value out of a DOM Element

I have been able to get xml.dom.minidom.parse('somefile.xml') and then
dom.getElementsByTagName('LLobjectID') to work to the point where I
get something like: [<DOM Element: LLobjectID at 0x13cba08>] which I
can get down to <DOM Element: LLobjectID at 0x13cba08but then I
can't find any way to just get the value out from the thing!

..toxml() returns something like: u'<LLobjectID><![CDATA[1871203]]></
LLobjectID>'.

How do I just get the 1871203 out of the DOM Element?

Thanks,

Sep 27 '07 #1
4 5120
kj7ny wrote:
I have been able to get xml.dom.minidom.parse('somefile.xml') and then
dom.getElementsByTagName('LLobjectID') to work to the point where I
get something like: [<DOM Element: LLobjectID at 0x13cba08>] which I
can get down to <DOM Element: LLobjectID at 0x13cba08but then I
can't find any way to just get the value out from the thing!

.toxml() returns something like: u'<LLobjectID><![CDATA[1871203]]></
LLobjectID>'.

How do I just get the 1871203 out of the DOM Element?
It contains a CDATA node which in turn contains a Text node (AFAIR), so you
have to walk through the children to get what you want.

Alternatively, try an XML API that makes it easy to handle XML, like
ElementTree (part of the stdlin in Python 2.5) or lxml, both of which have
compatible APIs. The code would look like this:

tree = etree.parse("some_file.xml")
id = tree.find("//LLobjectID")
print id.text

Stefan
Sep 27 '07 #2
On 27 Sep, 07:50, kj7ny <kj...@nakore.comwrote:
I have been able to get xml.dom.minidom.parse('somefile.xml') and then
dom.getElementsByTagName('LLobjectID') to work to the point where I
get something like: [<DOM Element: LLobjectID at 0x13cba08>] which I
can get down to <DOM Element: LLobjectID at 0x13cba08but then I
can't find any way to just get the value out from the thing!

.toxml() returns something like: u'<LLobjectID><![CDATA[1871203]]></
LLobjectID>'.

How do I just get the 1871203 out of the DOM Element?
DOM Level 3 provides the textContent property:

http://www.w3.org/TR/DOM-Level-3-Cor...e3-textContent

You'll find this in libxml2dom and possibly some other packages such
as pxdom. For the above case with minidom specifically (at least with
versions I've used), you need to iterate over the childNodes of the
element, obtaining the nodeValue for each node and joining them
together. Something like this might do it:

"".join([n.nodeValue for n in element.childNodes])

It's not pretty, but encapsulating stuff like this is what functions
are good for.

Paul

Sep 27 '07 #3
Forgot to mention I'm using Python 2.4.3.

Sep 27 '07 #4
kj7ny wrote:
Forgot to mention I'm using Python 2.4.3.
You can install both lxml and ET on Python 2.4 (and 2.3). It's just that ET
went into the stdlib from 2.5 on.

Stefan
Sep 27 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Derek Cooper | last post by:
I hope you can help me. I posted this in the microsoft sql server newsgroup a few days ago and got no response so I thought I'd try here. If I can provide any clarification I'll be glad to do so....
0
by: Ingrid | last post by:
Am I right in thinking that datatyping at element level ie <xs:element name="num" type="xs:integer"> and specifying a choice of attribute values ie <xs:attribute name="kind"> <xs:simpleType>...
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
3
by: Lynn | last post by:
Hi all, I am having problem when did the validation of XML document with Schema. my schema is like the following: <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="schema.xsd"...
1
by: Maksim | last post by:
Trying to find out a way how to restrict value of the element by name of an element, it might be not even possible, but anyhow. Let's consider following snippet: <xs:element name="tag1"...
1
by: Paul Cheevers | last post by:
Hi, I've been trying to write some schema to validate the Value element of an Eq element but to no avail. If the Value Element is off type DateTime then the node should be able to contain text...
13
by: dbuchanan | last post by:
Hello, Here is the error message; ---------------------------- Exception Message: ForeignKeyConstraint Lkp_tbl040Cmpt_lkp302SensorType requires the child key values (5) to exist in the...
8
by: patrizio.trinchini | last post by:
Hi All, I'would like to write an XSL transformation that changes the value of the atribute of a given element according to the value of another atttribute of the same element. For instance,...
3
by: patrizio.trinchini | last post by:
Hi, how can remove sibling elements based on the value of an attribute ? For instance, gven the XML document: <root> <parentElment> <testElement name="A"> <removableElement/>
2
by: Bilal | last post by:
Hello, I'm stuck on this problem for quite some time and hope somebody would be able to guide me. Basically, I need to populate a large number of "template" XML files which have all...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.