The PowerHouse
The PowerHouse

Reputation: 560

How to create Object from XML using using `protocol buffer` and `protobuf-java-format` in java

I am creating a sample program using protocol buffer and protobuf-java-format.My proto file is

package com.sample;

option java_package = "com.sample";
option java_outer_classname = "PersonProtos";

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;
}

My Sample program is

package com.sample;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;

import com.google.protobuf.Message;
import com.googlecode.protobuf.format.XmlFormat;
import com.sample.PersonProtos.Person;

/**
 * This class generate XML out put from Object and vice-versa
 * 
 * @author mcapatna
 * 
 */
public class Demo
{
    public static void main(String[] args) throws IOException
    {

        // get the message type from protocol buffer generated class.set the
        // required property
        Message personProto = Person.newBuilder().setEmail("a").setId(1).setName("as").build();
        // use protobuf-java-format to generate XMl from Object.
        String toXml = XmlFormat.printToString(personProto);
        System.out.println(toXml);
        // Create the Object from XML
        Message.Builder builder = Person.newBuilder();
        String fileContent = "";
        Person person = Person.parseFrom(new FileInputStream("C:\\file3.xml"));
        System.out.println(XmlFormat.printToString(person));
        System.out.println("-Done-");
    }

}

XmlFormat.printToString() is working fine.but creating object from XML not working I also tried XmlFormat.merge(toXml, builder); .but since merge() return void.so how can we get the object of Person class. Both the above method merge() and parseFrom() giving the same exception com.google.protobuf.InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag.

NOTE: "C:\\file3.xml" have the same content as toXml.

Upvotes: 4

Views: 6161

Answers (1)

The PowerHouse
The PowerHouse

Reputation: 560

After a lots of effort,I found the solution...Here is the answer

package com.sample;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import com.google.protobuf.Message;
import com.googlecode.protobuf.format.XmlFormat;
import com.sample.PersonProtos.Person;

/**
 * This class generate XML output from Object and vice-versa
 * 
 * @author mcapatna
 * 
 */
public class Demo
{
    public static void main(String[] args) throws IOException
    {
        long startDate=System.currentTimeMillis();


        // get the message type from protocol buffer generated class.set the
        // required property
        Message personProto = Person.newBuilder().setEmail("a").setId(1).setName("as").build();
        // use protobuf-java-format to generate XMl from Object.
        String toXml = XmlFormat.printToString(personProto);
        System.out.println("toXMl "+toXml);
        // Create the Object from XML
        Message.Builder builder = Person.newBuilder();
        String fileContent = "";
//      file3 contents same XML String as toXml
        fileContent = readFileAsString("C:\\file3.xml");
        // call protobuf-java-format method to generate Object
        XmlFormat.merge(fileContent, builder);
        Message msg= builder.build();
        System.out.println("From XML"+XmlFormat.printToString(msg));
        long endDate=System.currentTimeMillis();
        System.out.println("Time Taken: "+(endDate-startDate));
        System.out.println("-Done-");
    }

    private static String readFileAsString(String filePath) throws IOException
    {
        StringBuffer fileData = new StringBuffer();
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1)
        {
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
        }
        reader.close();
        return fileData.toString();
    }
}

Here is the Output of program:

toXMl <Person><name>as</name><id>1</id><email>a</email></Person>
From XML<Person><name>Deepak</name><id>1</id><email>a</email></Person>
Time Taken: 745
-Done-

Hope it will be useful for other members.

Upvotes: 6

Related Questions