By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,643 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

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

P: 12
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>indonesia</country>
<hobby>
<most>surfing</most>
<little>gardening</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
Nov 6 '08 #1
Share this Question
Share on Google+
1 Reply


P: 3
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.

Expand|Select|Wrap|Line Numbers
  1. TxtFileData = [aLine.strip() for aLine in open('data.txt').readlines()]
  2.  
  3. while "" in TxtFileData: TxtFileData.remove("")
  4. ## To remove any line in between if there is no data
  5.  
  6. XmlData = """
  7. <people>
  8. <profile>
  9. <name>%s</name>
  10. <home>%s</home>
  11. <age>%s</age>
  12. <country>%s</country>
  13. <gender>%s</gender>
  14. <hobby>
  15. <most>%s</most>
  16. <little>%s</little>
  17. </hobby>
  18. </profile>
  19. </people>
  20. """%(TxtFileData[0],TxtFileData[1],TxtFileData[2],TxtFileData[3],TxtFileData[4],TxtFileData[5],TxtFileData[6])
  21.  
  22. XmlFile = open('data.xml','w')
  23. XmlFile.write(XmlData)
  24. XmlFile.close()
  25.  
  26.  
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"
Nov 8 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.