Protocol buffer convert to xml

1,816 views
Skip to first unread message

Pradeep Rathnayaka

unread,
Mar 25, 2013, 5:26:19 AM3/25/13
to prot...@googlegroups.com
I spent whole day to convert a protocol buffers message into an XML file(should be done both direction xml-proto, proto-xml), unfortunately I couldn't do that as expected. I have used here JDOM2 as xml parser. please anyone help or suggest me to produce XML/Proto message using protocol buffers?

My protocol Message as below,
message RoutePoint {
    required double x = 1;
    required double y = 2;
    optional double z = 3;
    optional double orientation = 4;
}

message WayPoint {
    required uint32 route_id = 1;
    required RoutePoint point = 2;
}

message RouteWay {
    optional uint32 command_ref = 1;
    required uint32 grant_id = 2;
    optional WayPoint begin_point = 3;
    required WayPoint end_point = 4;    
}

I tried with JAXB to Marshalling and Unmarshalling but is also doesn't support to this. 
Appreciate if provide an example for my case.

PraYes

unread,
Apr 11, 2013, 11:26:02 PM4/11/13
to prot...@googlegroups.com
Hi Binh Dao Duy,
It works fine for me, 
Anyway, I used JDOM-2 for manipulating XML data, Below is my implemented code portion

/* Generating a XML file from Proto the message. */
public void proto2Xml(Message message) {
SAXBuilder builder = new SAXBuilder();
Document document;
XMLOutputter outputter;
try {
document = builder.build(new ByteArrayInputStream(XmlFormat.printToString(message).toString().getBytes()));
outputter = new XMLOutputter();
outputter.setFormat(Format.getPrettyFormat());
System.out.println("++++++++PROTO2XML++++++++++++++++++++");
outputter.output(document, System.out); // XML Console out.
System.out.println("++++++++++++++++++++++++++++++++++++++");
outputter.output(document, new FileWriter(xmlFilePath));
} catch (JDOMException e) {
System.out.println("JDOMException : " + e.getMessage());
} catch (IOException e) {
System.out.println("IOException : " + e.getMessage());
}
} // End of proto2Xml.


Thanks,
Pradeep

On Monday, March 25, 2013 7:00:17 PM UTC+8, Binh Dao Duy wrote:
I think you should try with this https://code.google.com/p/protobuf-java-format/

Vào 16:26:19 UTC+7 Thứ hai, ngày 25 tháng ba năm 2013, PraYes đã viết:
Reply all
Reply to author
Forward
0 new messages