1. Try the following: Create a class called AbstractPayload which is an XML enabled abstract class. Inherit all payload objects from this class. Change the type of the payload list property to be AbstractPayload and add the XMLPROJECTION="element" property parameter to the payload property. The using RootObject for wrapper should do all that you need.
2. If the different payloads are in different namespaces, manage this by using the NAMESPACE parameter in each class to specify the XML namespace that the class should be in. 3. If you are building these pieces up not using classes, just use RootElement for the top level containing element. Then use Element method for each of the other tags. 4. The way to get the embedded classes (like Address) to be automatically instantiated is to make them serial classes. This works with either registered or persistent objects. Sample.Address in Sample.Person is an example with persistent classes, but he same works for subclasses of %RegisteredObject. See Sample package that is in the SAMPLES namespace. 5. In some cases, the write command is just about as easy. However, most of the time, using %XML.Writer gives a more descriptive approach with less code and easier maintenance as well as automatic handling of XML namespaces. Marvin "Wendy Griffiths" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Sorry but this is a long message. > I am trying to output a number of XML messages using the %XML.Writer class. > I am using version 5.0.8. of Cache. I had schemas of the messages I want to > create so I created my classes by importing the schemas. There is one > outer wrapper message that I am trying to create. The message contains an > element which repeats. The schema import creates these with "Collection = > list " against the property. This element is a place holder for the > payload XML message content to be inserted into it. The element has a > property called any and the import has defined it as %XML.String with an > XMLPROJECTION of "ANY" and [ SqlFieldName = _any ] on the end. So the > purpose of this property is to be able to contain another XML message (of > which there are many types). So the basic message format is: > > <wrapper_message> > <payload> > <content_message> > </content_message> > </payload> > <payload> > <another_message> > </another_message> > </payload> > </wrapper_message> > > Where payload is the repeating property and the any property is the content > messages. Another requirement I have to cope with is that the contents > messages have their own namespace definitions that I wish to output. Using > the classes that have been generated, to create a new payload message I do > the following: > > s status=wrapper.payload.InsertAt(##class(package.payload).%New(),1) > s wrapper.payload.GetAt(1).any=##class(package.messagename).%New() > > but when I do a > s status=XMLWriter.RootObject(wrapper) > > I just get: > > <?xml version="1.0" encoding="UTF-8"?> > <wrapper> > <payload> > [EMAIL PROTECTED] > </payload> > <payload> > [EMAIL PROTECTED] > </payload> > </wrapper> > > i.e. the payload content just comes out as the object reference and > assumming I could get the message rather than the reference coming out I > still don't know how I would get the namespace definitions to come out on > each of the content messages. > I have tried splitting it into bits of the message, using multiple > XML.Writer's to write out the separate bits and it nearly works. I have to > do a RootElement method to write out the wrapper start tag, another > RootElement method (with another writer) to write out the start payload tag, > then with another writer do a RootObject on the first message content, then > do an EndRootElement to write the end payload tag, repeate RootElement, > RootObject and EndRootElement for the next payload message, then use an > EndRootElement to close the wrapper message tag. Plus there is still a > problem in that when I use the RootElement method, non of it's attached > attributes come out. > > But the code doesn't feel right anyway, it feels like I'm going all round > the houses just to arrive next door. It's not just using the Writer, it's > also the setting of the object references. I find that I have to create new > references for all the sub-objects of an object, so if I have a class called > person, which has a property called address, which is itself an object with > properties I have to create a new instance of person, then create a new > instance of address to set to the address property of person. Which creates > tons of code. When I imported my schemas I chose not to have them as > persistant classes as I will never need to store the data, I am just using > them to create XML messages and read them in. If I had imported the > messages and selected to have persistant classes, if I created a new > instance of person, would the address object then be automatically > instantiated, so I could just start setting the sub-object's properties > without creating it? > > So, I have a few problems: > Is the definition of the XML classes the most suitable for what I want or > would they be better another way? > How would be the best way to use the XML.Writer to try to easily achieve > what I want? > Should I re-import my schemas and choose persistant classes? > Is it really going to save me time using these new-fangled contraptions, > like classes and XML.Writers or would it be quicker and easier constucting > the output using the good old WRITE command? > > If you can answer any of my problems I'd be most thankful. > > thanks > Wendy Griffiths > >
