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

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

P: 4
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.
Thankyou
Dec 18 '08 #1
Share this Question
Share on Google+
4 Replies


P: 4
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

P: 4
bump please help me!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Dec 28 '08 #3

P: 4
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

<root>
<weapon>P228
<price>468
</price>
</weapon>
<weapon>USP
<price>500
</price>
</weapon>
</root>

if this is the xml file, then i want the application to print,
<root>
<weapon>
<price>

Thanks.
Dec 28 '08 #4

bvdet
Expert Mod 2.5K+
P: 2,851
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
  2.  
  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
  10.  
  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>'''
  21.  
  22. docXML = minidom.parseString(doc)
  23.  
  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
Output:
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.