/pb2xml

Python and C++ XML formating of Google Protobuf messages.

Primary LanguageC++BSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

pb2xml

The pb2xml project is designed to output Google protobuf messages to xml.

Python

The python message protobuffer bindings can be created using the --python_out flag in the protobuf compiler.

Then the py.xml_format class can be used to generate xml output from generated protobuf message objects.

Assume in the following example that the addressbook_pb2 object has been generated using the addressbook.proto definition.

# Create the python message object
> person = addressbook_pb2.Person()
# (add various components of the person object)
> person.id = "100"
> ...
# Create a string of xml with an utf-8 encoding attribute
> string_output = xml_format.MessageToXml(person,encoding="utf-8")
# Then any string based functions can be applied to the string
> print string_output
<?xml version="1.0" encoding="utf-8"?><Person> ... </Person> # Can also get the DOM object directly
> dom = xml_format.MessageToDOM(person)
# and then all dom related functions can be used
> print xml_format.MessageToDOM(person).toprettyxml(indent=" ")
<?xml version="1.0" ?>
<Person>
...
</Person>

A addressbook.proto sample protobuf definition is in the test/proto directory.

C++

The C++ method "xml_format.cc" borrows heavily on the text_format.cc class defined at the google code repository: text_format.cc.

RapidXML was choosen to encode the XML protobuf DOM object. Alternate xml libraries could be used (just check the source and replace with library of your choice).

To convert the protobuf object into an xml rapidxml::xml_document<> document object use the void XmlFormat::Printer::MessageToDOM(const Message& message, rapidxml::xml_document<>* doc) method. The method void XmlFormat::Printer::PrintToXmlString(const Message& message, string* output) modifies the string output with the contents of the generated DOM object.

Given an instance of the protobuf example message definition, addressbook.proto as the variable tutorial::AddressBook address_book:

$ cout << address_book.DebugString() << endl;
person {
name: "name1"
id: 12
email: "asdf@example.com"
phone {
number: "222203333"
type: WORK
}
}
person {
name: "Second Name"
id: 2
}

And the equivalent in XML:

$ string debug_string;
$ google::protobuf::XmlFormat::PrintToXmlString(address_book, &debug_string);
$ cout << debug_string << endl;`

<?xml version="1.0" encoding="utf-8"?>
<AddressBook>
<person>
<name>name1</name>
<id>12</id>
<email>asdf@example.com</email> <phone>
<number>222203333</number>
<type>WORK</type>
</phone>
</person>
<person>
<name>Second Name</name>
<id>2</id>
</person>
</AddressBook>

Note that other RapidXML printing methods can be used, including removing the whitespace from the xml using the print_no_indenting on the DOM object.

Also if the user wants to output the xml directly (rather than using a DOM model to contain the object heirarchy, you need to write your own TextGenerator class as per the text_format.cc.