Parsing an xml response from a url using STAX

时光总嘲笑我的痴心妄想 提交于 2019-12-11 07:34:37

问题


I am sending a xml request through java code and getting the xml response using the below code:

BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = rd.readLine()) != null) {
        sb.append(line + '\n');
    }

Now I need to parse the xml response using STAX so I have written a method for parsing:

XMLReader reader = new XMLReader();
ArrayList<String> List = reader.parse(connection.getInputStream());

and in the parse() I have reader = factory.createXMLEventReader(connection.getInputStream());

However I am getting the following error:

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:593)
at com.sun.xml.internal.stream.XMLEventReaderImpl.nextEvent(XMLEventReaderImpl.java:85)

Can someone please advise me where I am going wrong here?

Thanks,

swati


回答1:


Sorry the question is similar to this one : Error while parsing XML file with StAx.

Since before parsing, I was reading the content, the parsing was failing. As once you read the input stream, the stream closes and for parsing there was nothing to read.

Yes Ed, I was trying to read the same response twice.



来源:https://stackoverflow.com/questions/6580715/parsing-an-xml-response-from-a-url-using-stax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!