471,348 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

checking whether an xml.dom node already exists

gundarap
Hello all,
I'm working on minidom.
My goal is to see whether an element already exists in the xml file before adding. I was using getElementsByTagName() to check weather the element already exists.
The code looks something like this:
Value = argv[1]
Expand|Select|Wrap|Line Numbers
  1. a = dom.getElementsByTagName(Name)[0].childNodes[0].nodeValue
  2.      if (a!=Value):
  3.         add elements.
  4.     else:
  5.         return
This part gives me index error index out of range.
Can you people suggest me a good solution.
Is there any other method which I can use to find the element already exists or not in an xml file.
Thanks in advance.
Aug 27 '07 #1
4 12632
bartonc
6,596 Expert 4TB
To find out which list is giving the index error, break up that one-liner (always preferable):
a = dom.getElementsByTagName(Name)[0].childNodes[0].nodeValue
should be [example only - I'm not looking at the interface that you are using]:
Expand|Select|Wrap|Line Numbers
  1. elements = dom.getElementsByTagName(Name)
  2. if elements:
  3.     parentOfNodes = elements[0]
  4.     nodeList = parentOfNodes.childNodes
  5.     zerothNode = nodeList[0]
  6.     thisNodeValue = zerothNode.nodeValue
  7. else:
  8.     print "empty list"
That way when an error occurs, you'll know which list has is empty.

Next step would be to wrap each one in if statements or try blocks.
Aug 27 '07 #2
Sorry for the late reply.
That was really great.
I found one solution to check whether the node exists by using the getElementsBytagName method.I used this simple if statement to getrid of index error.
if(len(dom.getElementsByTagName(Name))!=0):
print "already exists"
This worked well.
Thanks for your valuable suggestions.
I will try to follow your suggestions to avoid this type of errors.


To find out which list is giving the index error, break up that one-liner (always preferable): should be [example only - I'm not looking at the interface that you are using]:
Expand|Select|Wrap|Line Numbers
  1. elements = dom.getElementsByTagName(Name)
  2. if elements:
  3.     parentOfNodes = elements[0]
  4.     nodeList = parentOfNodes.childNodes
  5.     zerothNode = nodeList[0]
  6.     thisNodeValue = zerothNode.nodeValue
  7. else:
  8.     print "empty list"
That way when an error occurs, you'll know which list has is empty.

Next step would be to wrap each one in if statements or try blocks.
Sep 4 '07 #3
bartonc
6,596 Expert 4TB
Sorry for the late reply.
That was really great.
I found one solution to check whether the node exists by using the getElementsBytagName method.I used this simple if statement to getrid of index error.
if(len(dom.getElementsByTagName(Name))!=0):
print "already exists"
This worked well.
Thanks for your valuable suggestions.
I will try to follow your suggestions to avoid this type of errors.
I need to know which module you are using, so that I can give this thread a better title. Thanks.

[EDIT: never mind. I found it.]
Sep 4 '07 #4
I used xml.dom.minidom..
Sep 5 '07 #5

Post your reply

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

Similar topics

67 posts views Thread by Steven T. Hatton | last post: by
1 post views Thread by Siu | last post: by
5 posts views Thread by Richard L Rosenheim | last post: by
14 posts views Thread by Kayle | last post: by
15 posts views Thread by Geiregat Jonas | last post: by
26 posts views Thread by Army1987 | last post: by
1 post views Thread by Ronak mishra | last post: by

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.