使用StAx解析XML文件时出错

我用StAx编写了一个xml解析器,用于解析从服务器接收的XML流。这是我的代码:

private Map parse(InputStream is) throws XMLStreamException { XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader reader = factory.createXMLStreamReader(is); boolean action = false; Map actionsMap = new HashMap(); while(reader.hasNext()){ int type = reader.next(); switch(type){ case XMLStreamReader.START_ELEMENT : action = reader.getLocalName().equals("action-description"); break; case XMLStreamReader.CHARACTERS : if( action ){ String act = reader.getText(); System.out.println("Action trouvées " + act); String[] praxiscmd = act.split("_"); if("CREATE".equals(praxiscmd[0])){ Create c = new Create(praxiscmd[1], praxiscmd[2], null); actionsMap.put(praxiscmd[1], c); } else if("DELETE".equals(praxiscmd[0])){ Delete d = new Delete(praxiscmd[1],praxiscmd[2], null); actionsMap.put(praxiscmd[1], d); } else if ("ADDPROPERTY".equals(praxiscmd[0])) { AddProperty ap = new AddProperty(praxiscmd[1], praxiscmd[2], praxiscmd[3], null); actionsMap.put(praxiscmd[1], ap); } else if ("ADDREFERENCE".equals(praxiscmd[0])) { AddReference ar = new AddReference(praxiscmd[1], praxiscmd[2], praxiscmd[3], null); actionsMap.put(praxiscmd[1], ar); } else if ("REMPROPERTY".equals(praxiscmd[0])) { RemProperty rp = new RemProperty(praxiscmd[1], praxiscmd[2], praxiscmd[3], null); actionsMap.put(praxiscmd[1], rp); } else if ("REMREFERENCE".equals(praxiscmd[0])) { RemReference rr = new RemReference(praxiscmd[1], praxiscmd[2], praxiscmd[3], null); actionsMap.put(praxiscmd[1], rr); } } } } 

我在行上得到这个错误: int type = reader.next()

  javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1] Message: Premature end of file. at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:584) at fr.lip6.graphelex.TelexImpl.parse(TelexImpl.java:147) at fr.lip6.graphelex.TelexImpl.sendHttpRequest(TelexImpl.java:264) at fr.lip6.graphelex.TelexImpl.schedules(TelexImpl.java:116) at fr.lip6.graphelex.MapperImpl.send(MapperImpl.java:92) at fr.lip6.graphelex.GraphElexAgent.executeCycle(GraphElexAgent.java:81) at praxis.guidance.agent.Agent.run(Agent.java:71) at java.lang.Thread.run(Thread.java:636) 

我不明白是什么问题,因为我对另一个案例使用相同的解析器,它完全有效。 以下是我从服务器收到的XML流的示例:

     GETLASTSCEDULES 200 last schedule returned   0 /telexDocuments/doc.dox   CREATE__8VtAMXv4EeCwaM2v2VqUyg_Model /telexDocuments/doc.dox:Peer#server2:hephaistos:0:15      

有人可以提一些建议吗?

编辑 :我可以找到我的问题的答案。 问题是当我从服务器收到答案作为InputStream时,我读了解析它。 您可能知道,在Java中,一旦读取了InputStream,它就会自动关闭。 我们忘记的事情。 谢谢你的文档

严格来说,因为答案比通过评论阅读更容易阅读….

迪米特里的回答


我可以找到我的问题的答案。 问题是当我从服务器收到答案作为InputStream时,我解析它。 您可能知道,在Java中,一旦解析了InputStream,它就会自动关闭。 我们忘记的事情。 谢谢你的文档 。

答案很简单。 在我的程序中,在我调用我解析的方法之前,我用来显示内容输入Stream以查看我收到的内容。 事实是,一旦你阅读/解析你的inpustream,它就会自动关闭。 请参阅以下链接。 因此,当我调用我的方法解析时,Inputstream参数已经关闭,这就是我捕获此错误的原因。

端点URL应该与?wsdl一起使用。 例如http://172.16.31.132:8088/mockApplicationServicesBinding?wsdl