Reflection::AddMessage() will automatically allocate the correct type -- you
don't have to do it yourself.  In fact, you aren't allowed to do it
yourself.  (The Message* which you pass to AddMessage() is supposed to point
at the *parent* message, just like with all the other reflection methods.)
In general, though, MessageFactory provides a way to allocate a Message
object for a given Descriptor.

http://code.google.com/apis/protocolbuffers/docs/reference/cpp/google.protobuf.message.html#MessageFactory

On Tue, Oct 13, 2009 at 2:35 PM, Jason Smith <jasonfsmi...@gmail.com> wrote:

>
> I wrote a generic proto-to-xml routine (using TinyXML) that'll convert
> any Message* into XML.  Thanks to the reflection interface and
> TextFormat, it was pretty straightforward.  (if there's interest in
> seeing it let me know... I'll clear it with my boss and post to this
> forum).
>
> Now I'm trying to do the reverse, and I'm having issues with repeated
> fields that are themselves messages.  When parsing the XML it finds a
> repeated message, but I can't figure out what method to call in order
> to automagically create a new object of the correct derived Message
> type for this repeated field.  The goal is to call
> Reflection::AddMessage(), then recursively call my XmlToProto()
> routine on this new object, parsing any children in the XML tree.
>
> I was looking for a method in either the Descriptor or Reflection
> interfaces to allocate a new object of the right Message-derived type
> (obviously it would have to then return it a a Message*, which is
> fine), but didn't see anything.  Can someone point me in the right
> direction?
>
> Thanks in advance,
> -Jason
>
> On Sep 29, 6:06 am, sim <simon.we...@gmail.com> wrote:
> > Cool. New syntax I didn't know about!  I think this would be useful
> > for converting the other way around (Proto-to-XML).
> >
> > On Sep 28, 6:05 pm, Kenton Varda <ken...@google.com> wrote:
> >
> > > Interesting.
> >
> > > Another way to do this would be to write code based on protobuf
> reflection
> > > and custom options, so you could have a proto like:
> > >   message Foo {
> > >     optional int32 i = 1 [(xml_disposition) = ATTRIBUTE];
> > >     optional Bar bar = 2 [(xml_disposition) = ELEMENT];
> > >   }
> >
> > > On Mon, Sep 28, 2009 at 6:14 AM, sim <simon.we...@gmail.com> wrote:
> >
> > > > Hi all. Would anybody be interested in an XML to Protocol Buffers
> > > > converter if it were opened up to the community?  I have an XML
> > > > stylesheet that transforms an annotated XSD set into a Java class
> that
> > > > uses JAXB and the Protocol Buffers Java API to convert XML documents
> > > > into either text or binary mode Protocol Buffers messages.  The XSD
> > > > annotations define the mappings from XSD elements to Proto messages
> > > > (although at present staying close to a 1:1 mapping is probably
> > > > safest). Some fancy XSD features are not yet supported but the usual
> > > > complexTypes, simpleTypes, elements, and enumerations work.
> >
> > > > Simon Weeks
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to