0

I am using python 2.7.2. and dom parser to read the data on several xml files. And export .db file to use on sql server. I have learned how to export the files, but XML tags are also included on my data tables. Here is the basic code :

from xml.dom import minidom
import sqlite3
xmldoc = minidom.parse('c:\dd\l2con\l2connection.xml')
coId = xmldoc.getElementsByTagName('id')

and each element are like this :

>>> coId[0]
<DOM Element: id at 0x249cf30>
>>> print coId[0].toxml()
<id>45859</id>
>>> coId[1]
<DOM Element: id at 0x24ac328>
>>> print coId[1].toxml()
<id>46889</id>
>>> 

where they should look like this: without the xml tags

>coId[0].toxml()
45859
>coId[1].toxml()
46889

Deleting the tags from xml probably won't work for this case.This way python can't read the xml file. Is there any way you can advise me on this subject.Any help will be appreciated.

Thank You

Yusuf

3 Answers 3

3

coId[0] is a DOM Element with one child (a Text node). Use

coId[0].firstChild.data

to get the text content.

This works too:

coId[0].firstChild.nodeValue
Sign up to request clarification or add additional context in comments.

1 Comment

thank you I have the correct values now. ` >>> coId[0].firstChild.data u'45859' >>> coId[0].firstChild.nodeValue u'45859' `
2

use coId[0].data instead, this gives the element content.

See mzjn answer, coId[0].firstChild.data is the correct way to do it.

4 Comments

This can't work. DOM Element instances do not have a data attribute.
Elements have a nodeValue attribute, but it returns None.
OK, linked to your answer then.
coId[0].data gives this error. AttributeError: Element instance has no attribute .data worked for other elements in similar project thanks
0

You could use/try the nodeValue attribute

Let's assume that you have:

myNode.toXml()= <name>Ben</name>

then

myNode.nodeValue = Ben

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.