XML to Protocol Buffers converter

9,066 views
Skip to first unread message

sim

unread,
Sep 28, 2009, 9:14:52 AM9/28/09
to Protocol Buffers
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

Kenton Varda

unread,
Sep 28, 2009, 1:05:15 PM9/28/09
to sim, Protocol Buffers
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];

sim

unread,
Sep 29, 2009, 6:06:17 AM9/29/09
to Protocol Buffers
Cool. New syntax I didn't know about! I think this would be useful
for converting the other way around (Proto-to-XML).

Kenton Varda

unread,
Sep 29, 2009, 3:08:20 PM9/29/09
to sim, Protocol Buffers
Well, either system can convert either way (hopefully).  The question is in which format the user prefers to write their schemas -- they should only have to write one.  Personally I'd prefer to write mine in .proto, but I'm probably biased.  :)

Jason Smith

unread,
Oct 13, 2009, 5:35:07 PM10/13/09
to Protocol Buffers
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

Kenton Varda

unread,
Oct 13, 2009, 7:52:07 PM10/13/09
to Jason Smith, Protocol Buffers
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.

Kartik

unread,
Nov 25, 2009, 12:37:37 PM11/25/09
to Protocol Buffers
Hi Jason
I'm trying to feed an application which only takes XML but I only
receive data as protocol buffers.
Would it be possible for you to share the proto-to-xml routine that
you made?
Thanks,

Kartik

On Oct 13, 4:35 pm, Jason Smith <jasonfsmi...@gmail.com> wrote:
> I wrote a genericproto-to-xmlroutine (using TinyXML) that'll convert
> any Message* intoXML.  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 theXMLit 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 theXMLtree.
>
> 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 aprotolike:
> > >   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 anXMLto Protocol Buffers

Alex Antonov

unread,
Dec 3, 2009, 1:00:27 PM12/3/09
to Protocol Buffers
Jason, there is a project that contains the XML & JSON marshalers for
protobuf and back which would do exactly what you want:
http://code.google.com/p/protobuf-java-format

Thanks,
Alex

tranchis

unread,
Dec 5, 2009, 6:15:29 AM12/5/09
to Protocol Buffers
Hi all,

Just out of need, we needed a converter from XSD to Thrift and we have
coded it in Java, using XSOM. It's available at http://github.com/tranchis/xsd2thrift

The way it's done, it would be really simple to encode the output for
Protocol Buffers as well. If you think it might be interesting, let me
know and I'll give it a try.

Best,

Sergio.

Aravinth Veeramuthu

unread,
Jul 16, 2015, 5:52:00 AM7/16/15
to prot...@googlegroups.com
How to convert the XML data into the Protocol buffer format in c++???

Michael Haberler

unread,
Jul 16, 2015, 6:52:37 AM7/16/15
to Aravinth Veeramuthu, prot...@googlegroups.com

> Am 16.07.2015 um 11:52 schrieb Aravinth Veeramuthu <aravinth....@vembu.com>:
>
> How to convert the XML data into the Protocol buffer format in c++???


https://github.com/search?utf8=%E2%9C%93&q=xml+protobuf&ref=simplesearch

>
> --
> You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
> To post to this group, send email to prot...@googlegroups.com.
> Visit this group at http://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/d/optout.

Huimin

unread,
May 17, 2016, 1:30:28 PM5/17/16
to Protocol Buffers
Hello Simon,
I am interested in the XML to Protocol Buffers converter. Is it opened up to the community? Where can I get it?
Thanks a lot for your support!
Huimin

Huimin

unread,
May 17, 2016, 1:30:28 PM5/17/16
to Protocol Buffers
Hello Simon,
Sorry! I forgot to mention that I am interested in XML to Protocol Buffers converter in java.
Thanks!
Huimin

On Monday, September 28, 2009 at 9:14:52 AM UTC-4, sim wrote:
Reply all
Reply to author
Forward
0 new messages