使用StAx解析XML文件时出现错误

11

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

private Map<String, IUnitaryAction> parse(InputStream is) throws XMLStreamException {

    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader reader = factory.createXMLStreamReader(is);
    boolean action = false;
    Map<String, IUnitaryAction> actionsMap = new HashMap<String, IUnitaryAction>();

    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 流的示例:
  <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  <responses>
   <executed-commands>
      <command><name>GETLASTSCEDULES</name>
      <status-code>200</status-code>
      <description>last schedule returned</description>
      </command>
     </executed-commands>
       <schedules><schedule><schedule-id>0</schedule-id>
       <doc-id>/telexDocuments/doc.dox</doc-id>
       <actions>
         <action>
          <action-description>CREATE__8VtAMXv4EeCwaM2v2VqUyg_Model</action-description>  
         <action-id>/telexDocuments/doc.dox:Peer#server2:hephaistos:0:15</action-id>
        </action>
      </actions>
      <non-actions/></schedule></schedules>
      <get-constraints/>
</responses>

能否给一些建议?

编辑:我可能已经找到了答案。问题出在我从服务器接收InputStream作为答案,然后进行读取解析。你可能知道,在Java中,一旦InputStream被读取解析,它会自动关闭。有时候我们会忘记这点。感谢文档


1
你应该用你找到的解决方案回答你的问题。这对其他人会很有帮助。 - ewan.chalmers
我目前遇到了这个问题,希望能看到解决方案。请提供。 - cmutt78
抱歉。我已经有一段时间没有连接到stackoverflow了。答案很简单。在我的程序中,在调用我解析的方法之前,我使用显示内容输入流以查看我接收到的内容。事实是,一旦你读取/解析你的输入流,它就会自动关闭。请参见下面的链接。因此,当我调用我的parse方法时,Inputstream参数已经关闭,这就是为什么我遇到这个错误的原因。 - Dimitri
@sudocode,没有正确的解决方案。我给出的javadoc链接上一切都很清楚。 - Dimitri
2个回答

6

严格来说,回答比阅读评论更容易理解...

Dimitri的回答


我可能已经找到了我的问题的答案。问题出在当我从服务器接收到InputStream作为答案时,我对其进行了解析。就像你可能知道的那样,在Java中,一旦InputStream被解析,它会自动关闭。有时候我们会忘记这些细节。感谢文档

答案非常简单。在我的程序中,在调用方法进行解析之前,我使用显示内容输入流来查看我正在接收的内容。事实上,一旦您读取/解析输入流,它就会自动关闭。请参见下面的链接。因此,当我调用我的解析方法时,Inputstream参数已经关闭,这就是我遇到这个错误的原因。


-1

1
你在说什么? - svichkar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接