On Thu, Feb 08, 2007 at 10:09:21PM +0000, Richard Lincoln wrote:
> Dear generateDS-users,
> 
> Please excuse also my naive questions, I have only been using python for two
> days.  

Richard -

Welcome to Python.  I hope you will enjoy it as much as I have.

> I have been using generateDS on my very simple schema with great
> success and have been able to parse my test xml files too.
> One thing I would like to be able to do is create an xml file that conforms
> to my schema from scratch, without having parsed any file.  For now I have
> just been using parseString() to create my root object like so-
> 
> rootObject = busbranch.parseString('<Network name=""/>')

That sounds like a very reasonable way to do it.

However, you could alternatively just use the class itself to
create an instance.  For example, suppose I had used the following
to generate files in generateDS-1.9a/Demos/People:

  ./generateDS.py -o tmp1sup.py -s tmp1sub.py --super=tmp1sup people.xsd

Then, I could create an instance and export it to XML with
something like the following:

  import tmp1sup
  p = tmp1sup.people()
  p.export(sys.stdout, 0)

Which would print out:

  <people>
  </people>

In some situations, this might be a better approach, because it
means that you can populate your tree.  For example, here are a few
additions that I have made in the subclass module tmp1sub.py
(generated above):

  class peopleSub(supermod.people):
      def __init__(self, person=None):
          supermod.people.__init__(self, person)
      def populate(self):
          person = personSub(id=22)
          person.populate()
          self.addPerson(person)
  supermod.people.subclass = peopleSub
  # end class peopleSub

  class personSub(supermod.person):
      def __init__(self, id=-1, value='', ratio_attr='', name='', ratio='', 
image$
          supermod.person.__init__(self, id, value, ratio_attr, name, ratio, 
imag$
      def populate(self):
          booster = boosterSub(firstname='Dave', lastname='Kuhlman')
          self.addPromoter(booster)
  supermod.person.subclass = personSub
  # end class personSub

Notice that I added "populate" methods.  Now I can use these
classes with something like the following:

  # test1.py

  import sys
  import tmp1sub

  def test():
      people = tmp1sub.peopleSub()
      people.populate()
      sys.stdout.write('<?xml version="1.0" ?>\n')
      people.export(sys.stdout, 0)

  if __name__ == '__main__':
      test()

Which will print out:

  <?xml version="1.0" ?>
  <people>
      <person id="22" value="" ratio_attr="">
          <name></name>
          <ratio></ratio>
          <category>-1</category>
          <promoter>
              <firstname>Dave</firstname>
              <lastname>Kuhlman</lastname>
          </promoter>
      </person>
  </people>

> 
> , but is there is a more elegant way?  To this and my xml files I hope then
> to add and insert new child nodes, but I am unsure how.  I see that
> .addChild() takes an argument 'value' and I imagine this is the child I wish
> to add, so I guess my question is how do I create this child object?  If you
> can give any other explanations or examples on how to use the various
> methods available to the rootObj, that too would be very much appreciated.

That's and excellent idea.  I really should work up and document a
few more examples.  In the meantime, you can look at the files in:

  generateDS-1.9a/Demos/People

and also look for suggestions in the "How-to Use the Generated
Source Code" section of the generateDS.py doc:

  http://www.rexx.com/~dkuhlman/generateDS.html#how-to-use-the-generated-source-
code

> 
> Again, Dave please excuse my very simple questions.  You seem to have many
> more interesting things to do with your time.  Thank you for generateDS, I
> was using JAXB before and having this has made for a very quick change to
> python.  I look forward to using its other, more advanced, features in the
> future.

I'm happy to answer questions and hope I can help.  And, this list
is certainly the appropriate place to ask questions about
generateDS.py.

Since you are new to Python, you may also want to know that the
following are good places to ask generic Python questions:

  http://dir.gmane.org/gmane.comp.python.tutor
  http://dir.gmane.org/gmane.comp.python.general

The tutor list is less heavily used, and the people there are very
helpful.

Hope this helps.  Please let me know when you have more questions.

Dave



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
generateds-users mailing list
generateds-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/generateds-users

Reply via email to