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


from that file i want make data.xml like this

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()]
  3. while "" in TxtFileData: TxtFileData.remove("")
  4. ## To remove any line in between if there is no data
  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])
  22. XmlFile = open('data.xml','w')
  23. XmlFile.write(XmlData)
  24. 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"
Nov 8 '08 #2

Post your reply

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