- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 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: Invalid byte 1 of 1-byte UTF-8 sequence.
它说我写错了消息。那么,写入方式是怎样的呢?
消息应该是:
<?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
}
最佳答案
不要将流包装在 ObjectOutputStream 中。它正在向流中添加您不需要的额外信息(至少根据您当前的代码)。而且,即使你确实需要它,你也不能这样使用它。
作为旁注/一般规则,您的输入流和输出流设置通常应该匹配。如果您出于某种原因确实需要使用ObjectOutputStream,则另一端需要一个ObjectInputStream来匹配它。
关于java - 将 Stax XML 写入 ObjectOutputStream(socket.getOutputStream) 时出现 MalformedByteSequenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5965791/
我正在尝试使用此 class 解析 XML 。当我输入一个简单的文件时,它工作得很好。 odp 这是我的主要 public static void main(S
我有这个代码: DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
我在解析 XML 文件时收到 MalformedByteSequenceException。 我的应用允许外部客户提交 XML 文件。他们可以使用任何受支持的编码,但大多数会根据提供给他们的示例在文件
我遇到了 com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException使用 XML 文件。我使用调试器逐步执行 X
我在运行使用 Rome Rss-parsing Lib 的 JSF 应用程序时遇到 MalformedByteSequenceException: [2017-08-31T15:54:01.241+0
我正在编写一个 XML 解析器类,当我运行它时,有时它工作正常但有时它不起作用并抛出此异常: MalformedByteSequenceException Invalid byte 1 of 1-by
我正在尝试使用以下代码解析 XML: DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilde
我正在尝试使用 Java 中的套接字从客户端应用程序向服务器发送 xml 消息,但我不知道如何将其写入流: String user = "Oscar" String p
public void openReport() { try { HashMap params = new HashMap(); params.put("aap
问题是:当我们尝试在 .xwt 文件的 xml 属性中使用俄语字符(例如标签文本)时,WindowBuilder 设计 View 崩溃并出现错误: com.sun.org.apache.xerces.
我有一个包含阿拉伯字符的 xml 文件。当我尝试解析一个文件时,出现异常,MalformedByteSequenceException:2 字节 UTF-8 序列的无效字节 2。我使用 POI DOM
我正在尝试解析包含希伯来字符的 XML 文件。我知道该文件是正确的,因为如果我(从不同的软件)输出没有希伯来语字符的文件,它解析得很好。 我尝试了很多方法,但总是出现这个错误 MalformedByt
我正在运行一个 selenium Web 驱动程序项目。我正在使用 Excel 工作表作为测试用例的输入。我收到以下错误 org.testng.TestNGException: com.sun.org
我得到这个 Exceptoin,问题是因为我输入的字符是 utf-8 编码的。 在我的 XML 文件的顶部说:我已经添加了编码。但我还是得到了这个异常(exception)。 //Set the f
我是一名优秀的程序员,十分优秀!