473,322 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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.
Thankyou
Dec 18 '08 #1
4 2600
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

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

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

Similar topics

5
by: Magnus Lie Hetland | last post by:
Is it possible to build a minidom tree bottom-up without access to a document node? It seems that simply instantiating the various node classes doesn't quite do the trick... -- Magnus Lie...
4
by: Jari Kujansuu | last post by:
I can successfully parse XML document using SAX or DOM and I can also validate XML document against schema. Problem is that my program should deal with user-defined schemas which means that when...
2
by: Stanimir Stamenkov | last post by:
I'm trying to find out if it is permissible to include a schema document with absent target namespace to a schema with specified target namespace, and if it is, what are the rules to resolve the...
2
by: PeterW | last post by:
I have an xml file from which I want to generate an xsd schema and at a later stage a cs class. The xml file has a mix of defined namespaces and also an empty namespace. These are defined as...
1
by: comic_rage | last post by:
Hi, I am writing code with C# to generate xml schema, however, the following code generate a UTF-16 in the first line. This will create some problems. How can get the code/missing what part of...
0
by: Richard Gregory | last post by:
Hi, I have the wsdl below, for an Axis web service, and when I select Add Web Refernce in Visual Studio the proxy is missing a class representing the returnedElementsType (see reference.cs below...
1
by: vidarno | last post by:
Hi! I'm working on a simple xml editor in Java at the moment. It's purpose is to enable the user to store information about typically collected types of posessions, like books, movies (DVDs)...
3
by: joshblair | last post by:
Hello, Has anyone ever seen or created such a code generator? I'm looking for a sample of a code generator that will generate code (preferably one that uses C# and the XMLTextWriter) to create...
5
by: kyosohma | last post by:
Hi all, I am attempting to create an XML document dynamically with Python. It needs the following format: <zAppointments reminder="15"> <appointment> <begin>1179775800</begin>...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.