4

我正在使用protocol buffer和创建一个示例程序。我的protobuf-java-format原型文件是

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;
}

我的示例程序是

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() 工作正常。但是从 XML 创建对象不起作用我也尝试过。但是XmlFormat.merge(toXml, builder);由于merge()返回 void.so 我们如何获取 Person 类的对象。上述方法merge()parseFrom()给出相同的异常 com.google.protobuf.InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag.

注意:"C:\\file3.xml"内容与toXml.

4

1 回答 1

5

经过一番努力,我找到了解决方案......这是答案

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();
    }
}

这是程序的输出:

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-

希望对其他会员有用。

于 2013-12-02T07:11:31.780 回答