Is any method available to convert a protobuf object to XML?

6,992 views
Skip to first unread message

yujian...@126.com

unread,
Jun 21, 2011, 7:27:25 AM6/21/11
to Protocol Buffers
Hey all;

Is any method available to convert a protobuf object to XML? The
backgound of this question is that original projects the all based on
XML to serial object, we need a bridge to convert current protobuf
object to XML runtime?

Questions:
1. Is this kind of solution availabe now?
2. If yes, how to? If not, could you give me some guideline how to
design and implement it?

Any idea please share, thanks in advance.

andy yu

unread,
Jun 21, 2011, 11:27:29 PM6/21/11
to prot...@googlegroups.com
Hey guys;

Is any implemented method available now? Otherwise I need to use TinyXML to achieve those tough convert issue?

Thanks.


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




--
Man, you should look forward to the future.

jianhua

unread,
Jun 23, 2011, 2:08:23 AM6/23/11
to Marc Gravell, prot...@googlegroups.com
Thanks Gravell's support on this issue.

In terms of the difficulities,  we try to use another solution, use protobuf to produce C++ object,  then convert C++ object to XML by serialization moudle on boost library.

At 2011-06-22 13:25:16,"Marc Gravell" <marc.g...@gmail.com> wrote:
There's no direct XML relationship to protobuf. Any available tooling will relate to your local platform / language / runtime / etc.

As it happens, I do this regularly in .NET using protobuf-net and XmlSerializer (the inbuilt .NET xml serializer) - which works great because protobuf-net has been designed to work with similar idioms. This, however, is a different API to the more typical Java / C++ etc clients, and I can' t comment on how easy it would be to serialize those as xml.

The fact that both are tree formats (rather than graph formats) means there aren't any fundamental problems - it is just a case of getting your chosen (and platform specific) xml serializer to understand the map.

Marc

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




--
Regards,

Marc


Lars Schouw

unread,
Jun 24, 2011, 11:29:05 AM6/24/11
to prot...@googlegroups.com
Yes, for example in C# you can do like this to create an XPathDocument using the protobuf-net lib.

public static XPathDocument Serialize(ProtocolBufferMesage msg)
        {
            XPathDocument xmlDoc = null;
            Serializer.PrepareSerializer<Test>();

            XmlSerializer x = new XmlSerializer(msg.GetType());
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (TextWriter w = new StreamWriter(memoryStream))
                {
                    x.Serialize(w, msg);
                    memoryStream.Position = 0;
                    xmlDoc = new XPathDocument(memoryStream);
                }
            }
            return xmlDoc;
        } 

Marc Gravell

unread,
Jun 24, 2011, 4:12:40 PM6/24/11
to prot...@googlegroups.com
The code shown uses XmlSerializer - it doesn't use protobuf-net at all. protobuf-net does tend to be friendly towards this, however you would:

- deserialize with protobuf-net into objects
- serialize with XmlSerializer

The only point of co tact between the two is the object model in the middle.
                    xmlDoc = new XPathDocument(memoryStream);c
                }
            }
            return xmlDoc;
        } 

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/protobuf/-/E_KNwOPbenoJ.

jianhua

unread,
Jun 26, 2011, 11:31:45 PM6/26/11
to prot...@googlegroups.com

Hey All;


Regression testing framework based on protobuf serialization format.

What does the framework need to achieve?

1.      Record testing cases and save them as protobuf in file.

2.      Read testing cases from file by protobuf format.

3.      Run testing cases, save the result in file by protobuf format, when all cases running over, then compare all the results with the expected ones which are also saved in file as protobuf format.

 

To achieve the above goals, I have the following questions.

1.      Is protobuf comparable? On file it saves in binary format, how to compare? Since we need to show the differences to developer?

2.      If we append all testing cases in one file, how to recover it?

3.     

 

The background is that my project uses XML as data serialization before, but now we replace it by protobuf, of course the testing framework should also change, but if change to protobuf, seems many tough problems need to fix. 

Possibly google has perfect solutions for the above issues, anybody who has the idea please attend and discuss, thanks all your help in advance.



jianhua

unread,
Jun 27, 2011, 5:27:30 AM6/27/11
to Marc Gravell, sch...@gmail.com, protobuf
Thanks Marc Gravell and Lars Schouw response on this issue.
 
Seems the corresponding XML serialization library isn't available for managed C++, so is it impossible to convert a protobuf object to XML so far.
 
I don't know whether google has a plan to write such plug-in and provide method ToXML()  then it will be very good.
To unsubscribe from this group, send email to protobuf+unsub...@googlegroups.com.

Lars Schouw

unread,
Jun 27, 2011, 10:03:26 AM6/27/11
to prot...@googlegroups.com
Accully I tried that (serialize from procotolbuffer to XML and back again from XML to protocolbuffer) but it fails, here is my source code.  I get an exception "ProtoBuf.ProtoException: Mismatched group tags detected in message" when I use protobuf-net.

The idea is to use XPath on the XML document, since procotolbuffers does not offer anything like that.
Might be slow but is flexible.



option optimize_for = SPEED;

//*************************
message
Test {
    repeated A a
= 1;
}

message A
{
    required
string str = 1;
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.XPath;
using ProtoBuf;
using TestMsg;

namespace protocolbufferserialize
{
   
class Program
   
{
       
static void Main(string[] args)
       
{
           
Test t = new Test();
           
XPathDocument xmldoc = Serialize(t);
           
Test t1 = Serialize(xmldoc);
       
}

       
public static XPathDocument Serialize(Test wro)

       
{
           
XPathDocument xmlDoc = null;
           
Serializer.PrepareSerializer<Test>();


           
XmlSerializer x = new XmlSerializer(wro.GetType());

           
using (MemoryStream memoryStream = new MemoryStream())
           
{
               
using (TextWriter w = new StreamWriter(memoryStream))
               
{

                    x
.Serialize(w, wro);

                    memoryStream
.Position = 0;
                    xmlDoc
= new XPathDocument(memoryStream);
               
}
           
}

           
return xmlDoc;
       
}

       
public static Test Serialize(XPathDocument xmlDoc)
       
{
           
Test t = null;
           
Serializer.PrepareSerializer<Test>();

           
XmlSerializer x = new XmlSerializer(xmlDoc.GetType());

           
using (MemoryStream memoryStream = new MemoryStream())
           
{
               
using (TextWriter w = new StreamWriter(memoryStream))
               
{

                    x
.Serialize(w, xmlDoc);
                    memoryStream
.Position = 0;
                    t
= Serializer.Deserialize<Test>(memoryStream);
               
}
           
}
           
return t;
       
}
   
}
}

Lars Schouw

unread,
Jun 27, 2011, 10:05:18 AM6/27/11
to prot...@googlegroups.com, Marc Gravell, sch...@gmail.com
When you say managed C++ what kind of library are you using? A C++ one? Can't you wrap that in a Managed C++ layer?

Ben Wright

unread,
Jun 27, 2011, 10:15:48 AM6/27/11
to prot...@googlegroups.com, Marc Gravell, sch...@gmail.com

Lars Schouw

unread,
Jun 27, 2011, 10:26:58 AM6/27/11
to prot...@googlegroups.com, Marc Gravell, sch...@gmail.com
Is there something simular for csharp or C++?

jianhua

unread,
Jun 27, 2011, 9:55:39 PM6/27/11
to prot...@googlegroups.com, Marc Gravell, sch...@gmail.com, protobuf helper guy

Thanks for your quick response.
Seems no easy and convenient solution for managed C++ which I mean it isn’t dependent on MS .NET framework.

If no such convert plug-in available, boost serialization and TinyXML are all my choice.  

If using boost, it is intrusive for original class, now I am responsible for a regression testing framework, using XML for object serialization,  the developing language is C++, seems they are all not easy to achieve the goal, the last choice is TinyXML, it could do, despite that many code need to written by  myself for many classes.

Hope to get more of your tips.  Thanks.


At 2011-06-27 22:05:18,"Lars Schouw" <sch...@gmail.com> wrote:
When you say managed C++ what kind of library are you using? A C++ one? Can't you wrap that in a Managed C++ layer?

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/protobuf/-/S6tfdGgl6IsJ.

jianhua

unread,
Jul 6, 2011, 4:59:11 AM7/6/11
to prot...@googlegroups.com, Marc Gravell, sch...@gmail.com
Thanks Wright;
Yes, protostuff has serialize tool for XML, but written by JAVA and my project is native C++.  

At 2011-06-27 22:15:48,"Ben Wright" <compuw...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/protobuf/-/hTS8SDZLVvgJ.
Reply all
Reply to author
Forward
0 new messages