4

protocol bufferと を使用してサンプル プログラムを作成しています。私のprotobuf-java-formatproto ファイルは

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()。上記の方法merge()parseFrom()同じ例外を与えることの 両方com.google.protobuf.InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag.

注:"C:\\file3.xml"と同じ内容toXmlです。

4

1 に答える 1