473,388 Members | 963 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,388 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 13026
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

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

Similar topics

67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
1
by: Siu | last post by:
Hi, in my ASP.NET (C#) application I would like to check in advance if a node exist: my code is as follow: XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(XML_FILENAME); XmlNode...
5
by: Richard L Rosenheim | last post by:
What's the proper technique for checking for the existence of an attribute within a node? Lets say I did a SelectSingleNode which returned this element: <AnAddress city="San Francisco"...
14
by: Kayle | last post by:
How should we check if the '\0' characters exists in the string as I am confused that some books mentioned that we have to check whether we need to make sure that we pass the...
15
by: Geiregat Jonas | last post by:
is using if(open("file",O_EXCL) != -1){ printf("File does exists")}else{printf("file does not exists"); } a good way of checking if a file exists or not, if not how should I do it ?
26
by: Army1987 | last post by:
Is this a good way to check wheter a file already exists? #include <stdio.h> #include <stdlib.h> int ask(const char *prompt); typedef char filename; int main(int argc, char *argv) { FILE...
0
by: jiing | last post by:
Hi all, I want to use sting(the same as Node.Text) to judge if a node exists in TreeView. I've tried several ways, but seems all failed. could anybody help me? Thanks in advance. //My...
8
by: Brad Pears | last post by:
I want to check to see if a particular object exists using vb.net 2005. I have a class called clsContract. When the user clicks the 'New Contract' button, I want to check to see if a clsContract...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.