Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: Dec 2008
Posts: 4
#1: Dec 18 '08
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

Newbie
 
Join Date: Dec 2008
Posts: 4
#2: Dec 18 '08

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


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.
Newbie
 
Join Date: Dec 2008
Posts: 4
#3: Dec 28 '08

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


bump please help me!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Newbie
 
Join Date: Dec 2008
Posts: 4
#4: Dec 28 '08

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


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.
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,564
#5: Dec 29 '08

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


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. >>> 
Reply

Tags
minidom, pyxml, schema, xml, xml.dom.minidom