The*_*use 4 java xml protocol-buffers
我正在使用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;
}
Run Code Online (Sandbox Code Playgroud)
我的示例程序是
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-");
}
}
Run Code Online (Sandbox Code Playgroud)
XmlFormat.printToString()工作正常。但是从 XML 创建对象不起作用我也尝试过XmlFormat.merge(toXml, builder);。但是由于merge()返回 void。所以我们如何获取 Person 类的对象。上面的方法都merge()给出parseFrom()了相同的异常
com.google.protobuf.InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag.
注意:"C:\\file3.xml"与 具有相同的内容toXml。
经过一番努力,我找到了解决方案......这就是答案
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();
}
}
Run Code Online (Sandbox Code Playgroud)
这是程序的输出:
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-
Run Code Online (Sandbox Code Playgroud)
希望它对其他成员有用。