how to make the deepest child in xml from data.txt

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ndoe
    New Member
    • Sep 2007
    • 12

    how to make the deepest child in xml from data.txt

    how to make the deepest child in xml from data.txt

    lindu
    bali
    23
    indonesia
    male
    surfing
    gardening

    from that file i want make data.xml like this
    <people>
    <profile>
    <name>lindu</name>
    <home>bali</home>
    <age>23</age>
    <country>indone sia</country>
    <hobby>
    <most>surfing </most>
    <little>gardeni ng</little>
    </hobby>
    </profile>
    </people>

    without change the code every file i free to make a new data.xml
    i mean i can make data.xml from data.txt without watch the content and i free to define the low for child in xml
  • nishi2rock
    New Member
    • Nov 2008
    • 3

    #2
    if all you want is to make another xml ( still a text file ) dont bother that it is xml and u can use just file operations to make the xml file.

    Code:
    TxtFileData = [aLine.strip() for aLine in open('data.txt').readlines()]
    
    while "" in TxtFileData: TxtFileData.remove("")
    ## To remove any line in between if there is no data
    
    XmlData = """
    <people>
    <profile>
    <name>%s</name>
    <home>%s</home>
    <age>%s</age>
    <country>%s</country>
    <gender>%s</gender>
    <hobby>
    <most>%s</most>
    <little>%s</little>
    </hobby>
    </profile>
    </people>
    """%(TxtFileData[0],TxtFileData[1],TxtFileData[2],TxtFileData[3],TxtFileData[4],TxtFileData[5],TxtFileData[6])
    
    XmlFile = open('data.xml','w')
    XmlFile.write(XmlData)
    XmlFile.close()
    it can be done in a much better way using xml.dom but this code will do if all you want is to convert the data to xml.

    Please note that i have added gender node
    Elaborate on "i free to define the low for child in xml"

    Comment

    Working...