471,311 Members | 1,909 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,311 software developers and data experts.

How can i generate a schema inferred from an xml document using xml.dom.minidom

Could somoene please guide me on how i would go about generating a schema using minidom inferred from an xml document that i have made.
Dec 18 '08 #1
4 2510
help. I basicly need to use minidom to extract the element names from an xml document and output them to another, to form a schema.
Dec 18 '08 #2
bump please help me!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Dec 28 '08 #3
I need to make an application to read an xml document, and extract the names between the tags and print them.
I do not want duplicates
so say


if this is the xml file, then i want the application to print,

Dec 28 '08 #4
2,851 Expert Mod 2GB
Using xml.dom.minidom, you can extract tag data using method doc.getElementsByTagName() and reading attribute firstChid.data. Note that "root" has no data.

Expand|Select|Wrap|Line Numbers
  1. from xml.dom import minidom
  3. def getTagData(doc, tag):
  4.     output = []
  5.     for elem in doc.getElementsByTagName(tag):
  6.         data = str(elem.firstChild.data).strip()
  7.         if data:
  8.             output.append(data)
  9.     return output
  11. doc = '''<root>
  12. <weapon>P228
  13. <price>468
  14. </price>
  15. </weapon>
  16. <weapon>USP
  17. <price>500
  18. </price>
  19. </weapon>
  20. </root>'''
  22. docXML = minidom.parseString(doc)
  24. for tag in ('root', 'weapon', 'price'):
  25.     data = getTagData(docXML,tag)
  26.     if data:
  27.         for item in getTagData(docXML,tag):
  28.             print "Tag '%s': %s" % (tag, item)
  29.     else:
  30.         print "No data to display for tag '%s'" % tag
Expand|Select|Wrap|Line Numbers
  1. >>> No data to display for tag 'root'
  2. Tag 'weapon': P228
  3. Tag 'weapon': USP
  4. Tag 'price': 468
  5. Tag 'price': 500
  6. >>> 
Dec 29 '08 #5

Post your reply

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

Similar topics

5 posts views Thread by Magnus Lie Hetland | last post: by
2 posts views Thread by PeterW | last post: by
1 post views Thread by vidarno | last post: by
5 posts views Thread by kyosohma | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.