java 使用 StAx 解析 XML 文件时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5976602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:48:20  来源:igfitidea点击:

Error while parsing XML file with StAx

javaxmlstax

提问by Dimitri

I wrote a xml parser with StAx that I use to parse XML streams received from the server.Here is my code :

我用 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);
                }
            }
        }
    }

I get this error on the line : int type = reader.next():

我在线上收到此错误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)

I don't understand what is the problem since i use the same parser for another case and it perfectly work. Here is an example of XML streams I received from a server :

我不明白有什么问题,因为我在另一个案例中使用了相同的解析器并且它完美地工作。这是我从服务器收到的 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>

Can someone give some advice ?

有人可以给一些建议吗?

EDIT: I may find the answer to my question. The problem was when I have received the answer from the server as InputStream, I readparse it. As you may know, in Java, once an InputStream is readparsed, it is close automatically. Things sometime we forgot. Thanks for the documentation

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

采纳答案by joekarl

Strictly because an answer is easier to read than the reading through the comments....

严格来说,答案比阅读评论更容易阅读......

Answer from Dimitri

迪米特里的回答



I may find the answer to my question. The problem was when I have received the answer from the server as InputStream, I parse it. As you may know, in Java, once an InputStream is parsed, it is close automatically. Things sometime we forgot. Thanks for the documentation.

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

The answer is very simple. In my program, before I called the method I parse, I use to display the content input Stream to see what I am receiving. The fact is, once you read/parse your inpustream, it is automatically close. See the link below. So when I call my method parse, the Inputstream parameter was already close, this is why I caught this error.

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

回答by vinoth

endpoint URL should be with ?wsdl. For example http://172.16.31.132:8088/mockApplicationServicesBinding?wsdl

端点 URL 应该带有 ?wsdl。例如 http://172.16.31.132:8088/mockApplicationServicesBinding?wsdl