473,804 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

4 New Member
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 2629
lostangel696
4 New Member
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
lostangel696
4 New Member
bump please help me!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!!!
Dec 28 '08 #3
lostangel696
4 New Member
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 Recognized Expert Moderator Specialist
Using xml.dom.minidom , you can extract tag data using method doc.getElements ByTagName() 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
2169
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 Hetland "In this house we obey the laws of http://hetland.org thermodynamics!" Homer Simpson
4
2943
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 I parse some element from XML document I don't know until runtime the type of the element (it depends on the type used in user-defined schema). XML parser obviously validates that the value of the element is correct type but still I receive that...
2
2501
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 target namespace of the components from the included schema document. I'm confused because of the rules I read in the XML Schema spec <http://www.w3.org/TR/xmlschema-1/#element-element>: > If the <element> element information item has <schema>...
2
6935
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 follows: <silcn:silcn xmlns:silcn='http://silcn.org/200309' xmlns='http://xmlprobe.com/200312'> it contains an element <report> off the root and also a separate <Silcn:report> again off the root.
1
4186
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 the code in C# to generate a first line with UTF-8 instead of UTF-16? Thanks, public XmlSchema CreateSchemaRoot()
0
5116
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 the wsdl). This complex type is a collection of another complex type(elementType), and the Reference.cs has an array of these rather than the single returnedElementsType. If If I want to be able to obtain these elements from the SOAP response I...
1
1282
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) and music (CDs). I want the program to be able to accept various forms of posessions, so if I later create a new
3
2066
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 an XML document structure based on an XML file as input. I have to build some classes that allow me to generate some very complex/large/nasty XML documents for use in B2B exchange of data (like
5
2490
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> <duration>1800</duration> </appointment>
0
9593
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10595
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.