Convert category temp file to XML

Code for converting temp file created in last post to XML

import os
def searchforpapa(papa):
    x = open("temp.utsav", "r")
    papa = ': '+papa
    while(x.readline()):
        content=x.readline()
        if(content.find(papa)!=-1):
            return int(content[:int(content.find(':'))])
        else:
            return -1    
            
        
    
a = open("temp.utsav", "r")
b = open ("t.utsav", 'a')
id=1
handler = open("category.xml", "a")
handler.write("<root>")
handler.close()
xx=a.readlines()
for content in xx:
    for x in content: 
        if(x=='Providence'): 
            print x
            break
        if(x==':'):
            nodeid = int(content[:int(content.find(x))])
            name = content[int(content.find(x))+2:]
            parent = content[int(content.find(x))+2:]
            while (int(name.find("/"))!=-1):
                name=name[int(name.find("/"))+1:]
            
            parent=parent[:int(parent.find(name))]
            parent=parent[:len(parent)-1]
            parentid=searchforpapa(parent)
            nodeid=str(nodeid)
            nodeid=nodeid.strip()
            parentid=str(parentid)
            parentid=parentid.strip()
            name=str(name)
            name=name.strip()
            id=str(id)
            xml="<id>"+id+"</id><node><nodeid>"+nodeid+"</nodeid><parentid>"+parentid+"</parentid><name>"+name+"</name></node>"
            id=int(id)
            id+=1
            print id
            xml=str(xml) 
            handler = open("category.xml", "a")
            handler.write(xml)
            handler.close()
            

    
handler = open("category.xml", "a")
handler.write("</root>")
handler.close()


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: