473,809 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

checking whether an xml.dom node already exists

gundarap
5 New Member
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 getElementsByTa gName() 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 13117
bartonc
6,596 Recognized Expert Expert
To find out which list is giving the index error, break up that one-liner (always preferable):
a = dom.getElements ByTagName(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
gundarap
5 New Member
Sorry for the late reply.
That was really great.
I found one solution to check whether the node exists by using the getElementsByta gName method.I used this simple if statement to getrid of index error.
if(len(dom.getE lementsByTagNam e(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 Recognized Expert Expert
Sorry for the late reply.
That was really great.
I found one solution to check whether the node exists by using the getElementsByta gName method.I used this simple if statement to getrid of index error.
if(len(dom.getE lementsByTagNam e(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
gundarap
5 New Member
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
4290
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. Unfortunately these ad hominem rhetorts are frequently introduced into purely technical discussions on the feasibility of supporting such functionality in C++. That usually serves to divert the discussion from the technical subject to a discussion of the...
1
21930
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 nodeApplication = xmlDoc.SelectSingleNode(xmlPath); Before launching the instruction xmlDoc.SelectSingoleNode(xmlPath), how would like to check if my xmlPath exists in the Xml document... it is a soft
5
19539
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" state="CA" /> What's the best why of determining if the attribute zipcode exists in this node? If I try accessing the attribute with code like: ZipCode = myNode.Attributes("zipcode").Value
14
2957
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 'null-terminated-string' to some functions, such as 'atoi'. (We assume that the string was extracted using an array, not string literal) This program shows blank for '\0' for all 3 cases. Why ? And I was trying to pass some strings formed using an array, without...
15
114216
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
4961
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 *in, *out;
0
12794
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 TreeView is defined to has depth =3 only.
8
17719
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 object already exists and if so what the state of that object is before doing anything else. I thought I could use ... if IsNothing(clsContract) then 'somc code
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10387
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7662
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.