Java 将Stax XML写入ObjectOutputStream(socket.getOutputStream)获取格式错误的ByteSequenceException
我正在尝试使用Java中的套接字从客户端应用程序向服务器发送xml消息,但我不知道如何将其写入流:Java 将Stax XML写入ObjectOutputStream(socket.getOutputStream)获取格式错误的ByteSequenceException,java,xml,sockets,unicode,stax,Java,Xml,Sockets,Unicode,Stax,我正在尝试使用Java中的套接字从客户端应用程序向服务器发送xml消息,但我不知道如何将其写入流: String user = "Oscar" String pass = "1234" ObjectOutputStream oos = new ObjectOutputStream( socket.getOutputStream()); // Create a XMLOutputFactory
String user = "Oscar"
String pass = "1234"
ObjectOutputStream oos = new ObjectOutputStream(
socket.getOutputStream());
// Create a XMLOutputFactory
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
// Create XMLEventWriter
XMLEventWriter eventWriter = outputFactory
.createXMLEventWriter(oos);
// Create a EventFactory
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
XMLEvent end = eventFactory.createDTD("\n");
// Create and write Start Tag
eventWriter.add(eventFactory.createStartDocument());
eventWriter.add(end);
//Se crean los atributos del tag
ArrayList<Attribute> attributes = new ArrayList<Attribute>();
attributes.add(eventFactory.createAttribute("nickname", user));
attributes.add(eventFactory.createAttribute("password", pass));
eventWriter.add(eventFactory.createStartElement
("", "", "authenticate",attributes.iterator(), null));
eventWriter.add(end);
eventWriter.add(eventFactory.createEndElement("", "", "authenticate"));
eventWriter.add(eventFactory.createEndDocument());
?
如果我这样做,它会给我以下例外情况:
javax.xml.stream.XMLStreamException:com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException:1字节UTF-8序列的字节1无效
上面说我写错了信息。那么,写作的方式是什么呢
信息应该是:
<?xml version="1.0" encoding="UTF-8"?>" +
<authenticate nickname="Oscar" password="1234" />
及
//方法
@SuppressWarnings({“未选中”、“空”})
公共void readCommand(InputStream命令){
试一试{
//XML格式
XMLInputFactory inputFactory=XMLInputFactory.newInstance();
//Se crea un nuevo事件阅读器
XMLEventReader eventReader=inputFactory.createXMLEventReader(命令);
while(eventReader.hasNext()){
XMLEvent=eventReader.nextEvent();
if(event.isStartElement()){
StartElement StartElement=event.asStartElement();
//我是特内莫斯,我是科曼多人
if(startElement.getName().getLocalPart().equals(“身份验证”)){
CommandAuthenticate(startElement);
}
}
}
}
捕获(XMLStreamException e){
e、 printStackTrace();
}
}
公共无效命令验证(StartElement StartElement){
字符串昵称=null;
字符串密码=null;
//科曼多海滩酒店
迭代器属性=startElement
.getAttributes();
while(attributes.hasNext()){
Attribute=attributes.next();
if(attribute.getName().toString().equals(“昵称”)){
昵称=attribute.getValue();
}
if(attribute.getName().toString().equals(“密码”)){
password=attribute.getValue();
}
}
//这里我为接收到的数据调用正确的方法
}
不要将流包装在ObjectOutputStream中。它向流中添加了您不需要的额外信息(至少根据您当前的代码)。而且,即使你确实需要它,你也不能像这样使用它
作为旁注/一般规则,您的inputstream和outputstream设置通常应该匹配。如果出于某种原因确实需要使用和ObjectOutputStream,则另一端需要一个ObjectInputStream来匹配它。不要使用ObjectOutputStream。这是用于序列化的。你不是在做序列化。只需让XMLEventWriter直接写入套接字的流即可。同样,在读取端没有ObjectInputStream
失败的原因是ObjectOutputStream向流中添加了一个标头,而XML解析器对此感到窒息。非常感谢您的帮助。您也是,但Tom Anderson是第一位。无论如何谢谢你的帮助事实上,我想杰塔伯恩比我早几秒钟。奥斯卡,有些人建议,如果你得到几个同样好的答案,你应该接受一个,然后再投票给其他人。这是给所有答案一些认可的一种方式。是的,我想我是第一个,但生活还在继续
<?xml version="1.0" encoding="UTF-8"?>" +
<authenticate nickname="Oscar" password="1234" />
//the call
this.Interpreter.readCommand(socket.getInputStream());
//the method
@SuppressWarnings({ "unchecked", "null" })
public void readCommand(InputStream command) {
try {
// Fabrica de entrada de XML
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
// Se crea un nuevo eventReader
XMLEventReader eventReader = inputFactory.createXMLEventReader(command);
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
StartElement startElement = event.asStartElement();
// Si tenemos un comando authenticate
if (startElement.getName().getLocalPart().equals("authenticate")) {
CommandAuthenticate(startElement);
}
}
}
}
catch (XMLStreamException e) {
e.printStackTrace();
}
}
public void CommandAuthenticate (StartElement startElement){
String nickname = null;
String password = null;
// Se leen los atributos del comando
Iterator<Attribute> attributes = startElement
.getAttributes();
while (attributes.hasNext()) {
Attribute attribute = attributes.next();
if (attribute.getName().toString().equals("nickname")) {
nickname = attribute.getValue();
}
if (attribute.getName().toString().equals("password")) {
password = attribute.getValue();
}
}
//here i call the right method for the data received
}