Giter Site home page Giter Site logo

jcash / pb2xml Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tmarthal/pb2xml

0.0 1.0 0.0 72 KB

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

Home Page: pb2xml

License: BSD 3-Clause "New" or "Revised" License

HTML 40.93% C++ 57.30% Python 1.66% Shell 0.11%

pb2xml's Introduction

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: "[email protected]"
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>[email protected]</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.

pb2xml's People

Contributors

tmarthal avatar claws avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.