串行的SimpleXML只发送第一线 [英] Serializer SimpleXML just send first line
问题描述
我有一个问题要发送的Android和POST一个Servlet之间的文件中的XML。我使用(简单的XML )的序列化。
I have a problem trying to send a file xml between Android and a servlet with POST. I'm using (Simple XML) for the serializing.
我的servlet做回应Android的:
My servlet do the response to Android:
Serializer serial = new Persister();
OutputStream o = response.getOutputStream();
MyXML myXML = new MyXML();
myXML.setMyElement("test");
serial.write(myXML, o);
这应该我的XML直接发送到这样的客户,
It's supposed to send my xml directly to the client like this,
<MyXML>
<MyElement>test</MyElement>
</MyXML>
但它仅发送的第一行。然后,在Android端得到这个异常,因为它无法获得与元素的第二行。
But it only sends the first line . Then, on the Android side gets this exception because it can't get the second line with the Element.
WARN/System.err(490): org.simpleframework.xml.core.ElementException: Element 'MyElement' does not have a match in class java.lang.Class at line -1
我不明白为什么只序列化当我和OutputStream中做的第一线,因为它的工作原理,当我保存的文件而不发送,
I can't understand why it only serialize the first line when i'm doing it with OutputStream because it works when i'm saving on files without sending it,
Serializer serial = new Persister();
File file = new File("MyPath");
MyXML myXML = new MyXML();
myXML.setMyElement("test");
serial.write(myXML, file);
我需要做的是这样的而不是字节,只是为了避免设置响应内容长度。
I need to do it like that and not with bytes, just to avoid to set the response content length.
非常感谢,
修改:添加MyXML.class
EDIT: Adding MyXML.class
有是MyXML.class,
There is MyXML.class,
package part.myApp;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root(name="MyXML")
public class MyXML{
@Element(name="MyElement")
private String a;
public void setMyElement(String a){
this.a=a;
}
public String getMyElement() {
return a;
}
}
感谢。
推荐答案
在'A'可能是一个问题,私人的访问。 使用POJO选项:
Private access on 'a' might be a problem. Use the POJO options:
@Root(name="MyXML")
public class MyXML{
private String a;
@Element(name="MyElement")
public void setMyElement(String a){
this.a=a;
}
@Element(name="MyElement")
public String getMyElement() {
return a;
}
}
让我知道是否适合你。
Let me know if that works for you.
这篇关于串行的SimpleXML只发送第一线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!