checking whether an xml.dom node already exists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gundarap
    New Member
    • Aug 2007
    • 5

    checking whether an xml.dom node already exists

    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]
    Code:
    a = dom.getElementsByTagName(Name)[0].childNodes[0].nodeValue
         if (a!=Value):
            add elements.
        else:
            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.
    Last edited by bartonc; Aug 27 '07, 08:54 AM. Reason: Added [CODE][/CODE] tags.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    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]:[CODE=python]
    elements = dom.getElements ByTagName(Name)
    if elements:
    parentOfNodes = elements[0]
    nodeList = parentOfNodes.c hildNodes
    zerothNode = nodeList[0]
    thisNodeValue = zerothNode.node Value
    else:
    print "empty list"[/CODE]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.

    Comment

    • gundarap
      New Member
      • Aug 2007
      • 5

      #3
      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.


      Originally posted by bartonc
      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]:[CODE=python]
      elements = dom.getElements ByTagName(Name)
      if elements:
      parentOfNodes = elements[0]
      nodeList = parentOfNodes.c hildNodes
      zerothNode = nodeList[0]
      thisNodeValue = zerothNode.node Value
      else:
      print "empty list"[/CODE]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.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by gundarap
        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.]

        Comment

        • gundarap
          New Member
          • Aug 2007
          • 5

          #5
          I used xml.dom.minidom ..

          Comment

          Working...