Pyxml generate Schema *HELP!!*
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Pyxml generate Schema *HELP!!*

Hybrid View

  1. #1
    Join Date
    Feb 2009
    Posts
    9

    Arrow Pyxml generate Schema *HELP!!*

    Hi all, I don't know if this is in the right place but I am still rather green at XML stuff.

    I've been looking at xml and want to make a python program that can generate the xml schema instead of typing out the whole schema by hand. As far as I have seen, I need the pyxml program installed for the xml schema generation to work.

    I have made a simple xml doc based on the L4D2 game for trials:
    Code:
    <Left_4_Dead_2>
    
    	<Game_content>
    	
    		<characters>
    			<player_characters>Ellis</player_characters>
    			<player_characters>Rochelle</player_characters>
    			<player_characters>Coach</player_characters>
    			<player_characters>Nick</player_characters>
    			
    			<Infected>Tank</Infected>
    			<Infected>Witch</Infected>
    			<Infected>Hunter</Infected>
    			<Infected>Smoker</Infected>
    			<Infected>Boomer</Infected>
    			<Infected>Spiter</Infected>
    			<Infected>Jocky</Infected>
    			<Infected>Charger</Infected>
    			<Infected>General</Infected>
    		</characters>
    		
    		<Items>
    		
    			<aid>medpack</aid>
    			<aid>pills</aid>
    			<aid>Adrenaline shot</aid>
    			
    			<Melee_Weapons>Machete</Melee_Weapons>
    			<Melee_Weapons>Guitar</Melee_Weapons>
    			<Melee_Weapons>Frying Pan</Melee_Weapons>
    			<Melee_Weapons>Nightstick</Melee_Weapons>
    			
    			<Volatile>Pipe Bombs</Volatile>
    			<Volatile>Molitovs</Volatile>
    			<Volatile>Boomer Bile</Volatile>
    			
    			<Ranged_Weapons>
    				<Close_range>Shotgun</Close_range>
    				<Close_range>Auto Shotgun</Close_range>
    				<long_range>Sub-machine gun</long_range>
    				<long_range>Machine gun</long_range>
    				<long_range>AK47</long_range>
    				<long_range>Pistol</long_range>
    				<long_range>Rifle</long_range>
    			</Ranged_Weapons>
    			
    			<Ammo>Normal</Ammo>
    			<Ammo>Ignitable</Ammo>
    		
    		</Items>
    	
    	</Game_content>
    </Left_4_Dead_2>
    Looking into other information I have made a very rough section of code to start with but I need a lot of help finishing it:

    Code:
    from xml.SAX import make_parser
    from xml.SAX.handler import ContentHandler
    
    class l4d2Handler(ContentHandler):
         def __init__ (self,searchTerm):
             
    ///////////////////////////////
    ///////////////////////////////
    
         def startElement:
               return
    
         def endElement:
                return
    
    make_parser()
    parser=parse(open('xmldoc.xml'))
    where I've put ////// i know there should be something more there, the code is made of pieces from tutorials I found that didn't explain enough to complete the program, and this is what I have made.

    I really need some help with this, and I need to complete it asap

  2. #2
    Join Date
    Feb 2009
    Posts
    9

    Re: Pyxml generate Schema *HELP!!*

    If anyone is actually looking at this, I have made some advancement with help from a friend

    Code:
    from xml.sax import make_parser
    from xml.sax.handler import ContentHandler
    #import cgi
    
    class XmlDocHandler(ContentHandler):
        def __init__ (self):
            self.names = {}
            self.string = {}
            self.should_print = True
    
        def startDocument(self):
            print "Start of document stuff"
    
        def startElement(self, name, attrs):
            if not self.names.has_key(name):
                print "<xs:element name=\"" + name + "\">".strip()
                self.should_print = True
            else:
                self.should_print = False
            
    #            self.names[name] = None
    ##            if Game_content == 'characters':
    ##                self.humans = attrs.get('player_characters',"")
    ##                self.zombies = attrs.get('Infected',"")
    ##            elif items == 'items':
    ##                self.health = attrs.get('aid',"")
    ##                self.physical = attrs.get('Melee_Weapons',"")
    ##                self.throwable = attrs.get('Volatile',"")
    ##                self.Ranged_Weapons = attrs.get('Close_range',"")
    ##                self.Ranged_Weapons = attrs.get('long_range',"")
    ##                self.ammo_options = attrs.get('ammo',"")
    ##            return
    
        def characters(self, content):
    #        if not self.string.has_key(content):
            if self.should_print:
                print "<xs:pattern value=\"([a-z A-Z])*\"/>" + content +"\">".strip()
    #            print content.strip()
                #self.string.has_key(content)
    
        def endElement(self, name):
            if not self.names.has_key(name):
                print "</xs:element name=\"" + name + "\">".strip()
                self.names[name] = None
            return
    
    
    #FormData = cgi.FieldStorage()
    #searchTerm = FormData['humans'].value
    #searchTerm = FormData["zombies"].value
    #searchTerm = FormData["health"].value
    #searchTerm = FormData["physical"].value
    #searchTerm = FormData["throwable"].value
    #searchTerm = FormData["Ranged_Weapons"].value
    #searchTerm = FormData["ammo_options"].value
    
    parser = make_parser()
    curHandler = XmlDocHandler()
    parser.setContentHandler(curHandler)
    parser.parse(open('XMLFile.xml'))
    While this does something useful it doesn't produce a good schema, What I need it to do is if all the "names" in an element are strings, to print
    Code:
     <xs:pattern value=\"([a-z A-Z])*\"/>
    What it does at the moment is just print
    Code:
     <xs:pattern value=\"([a-z A-Z])*\"/>
    everytime it sees a "name" which as you can imagine is a bit too often and rather untidy.

    Also any information on getting the information to align itself (tab) correctly would be very useful

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center