Start New Discussion within our Software Development Community

Hey All,
am looking for a way to break an xml Tag line into tag and attributes. Say I have This line

<event time="12:30" value="This is a Sample Value" type="event">
</event>

I have this so far

# Open file to read
f=open("myTest2.xml")

#start reading File
for line in f:
 linestart=line.find("<")
 newline=line[linestart:len(line)]

#check for XML version

 if newline[0:2] == "<?":
  print "XML version Line : "+newline
 
#get start tag

 if newline[0:1] == "<" and newline[1:2] != "?":
  x=newline.find(" ")
  y=newline.find(">")
  if str(x) == "-1" :
   temp=newline[1:y] 
  else:
   temp = newline[1:x]
#get attribute list
   attributeList = newline[x:len(newline)-2]
   print attributeList.split(" ")

This bears:


I want to get:


Thanks in advance for any replies.

Ok Solved it.

def attribList(newline):
   attributeList = newline[x:len(newline)-2]
   attribs = attributeList.split("\"")
   j=0
   i=0
   attrib={}
   while j < len(attribs):
    if "=" in attribs[j]:
     aHead = attribs[j][1:len(attribs[j])-1]
     attrib[aHead] = attribs[j+1]
     attribs.remove(attribs[j+1])
     j += 1
     i += 2
     if i == len(attribs):
      j = len(attribs)
    else:
     j += 1
     i += 1
     if i == len(attribs):
      j = len(attribs)
   return attrib
This question has already been answered. Start a new discussion instead.