将Stax XML写入ObjectOutputStream(socket.getOutputStream)获取MalformedByteSequenceException

时间:2011-05-11 14:20:58

标签: java xml sockets unicode stax

我正在尝试使用Java中的套接字从客户端应用程序向服务器发送xml消息,但我不知道如何将其写入流:

        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());
我应该使用:

        eventWriter.flush();

如果我这样做,它会给我以下例外:

  

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" />

这是我在服务器上使用的代码,用于阅读消息:

//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

}

2 个答案:

答案 0 :(得分:1)

不要将流包装在ObjectOutputStream中。它正在为您不需要的流添加额外信息(至少,根据您当前的代码)。并且,即使你确实需要它,也不能像这样使用它。

作为旁注/一般规则,您的输入流和输出流设置通常应匹配。如果由于某种原因 需要使用和ObjectOutputStream,则需要在另一端使用ObjectInputStream来匹配它。

答案 1 :(得分:0)

不要使用ObjectOutputStream。这是为了序列化。你没有进行序列化。只需让XMLEventWriter直接写入套接字的流。同样,在读取端没有ObjectInputStream。

失败的原因是ObjectOutputStream向流添加了一个标头,XML解析器正在阻塞它。