2

Hi everyone am currently working on an application that needs to parse an xml document so as to authenticate users.Am using URLConnection class of the java.net.* package to connect to as specific URL which gives an returns its response in xml format. When i try to parse the document using jdom , i get the following error:
org.jdom2.input.JDOMParseException: Error on line 1: Premature end of file

Can anyone pinpoint the problem and assist me with a remedy? thanks, here is a section of my code

try {
  String ivyString = "http://kabugi.hereiam.com/?username=" + ivyUsername + "&password=" + ivyPassword;

  URL authenticateURL = new URL(ivyString);
  URLConnection ivyConnection = authenticateURL.openConnection();
  HttpURLConnection ivyHttp = (HttpURLConnection) ivyConnection;
  System.out.println("Response code ==>" + ivyHttp.getResponseCode());
  if (ivyHttp.getResponseCode() != 200) {
    ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid username or password!", ""));
    page = "confirm.xhtml";
  } else {
    BufferedReader inputReader = new BufferedReader(new InputStreamReader(ivyConnection.getInputStream()));
    String inline = "";
    while ((inline = inputReader.readLine()) != null) {
      System.out.println(inline);
    }
    SAXBuilder builder = new SAXBuilder();

    Document document = (Document) builder.build(ivyConnection.getInputStream());
    Element rootNode = document.getRootElement();
    List list = rootNode.getChildren("data");
    for (int i = 0; i < list.size(); i++) {
      Element node = (Element) list.get(i);
      System.out.println("Element data ==>" + node.getChildText("username"));
      System.out.println("Element data ==>" + node.getChildText("password"));

    }

    page = "home.xhtml";
  }
} catch (Exception ex) {
  ex.printStackTrace();
  // ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid username or password!", ""));
}
4

3 回答 3

4

看起来像它,因为您正在读取输入流两次。一次打印,然后构建文档。当您构建 Document 对象时,输入流已经被完全读取并在其末尾。尝试以下代码,它只读取一次流

        BufferedReader inputReader = new BufferedReader(new InputStreamReader(ivyConnection.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String inline = "";
        while ((inline = inputReader.readLine()) != null) {
          sb.append(inline);
        }

        System.out.println(sb.toString());
        SAXBuilder builder = new SAXBuilder();

        Document document = (Document) builder.build(new ByteArrayInputStream(sb.toString().getBytes()));
于 2012-09-18T01:52:17.573 回答
1

我以前遇到过类似的问题。如果它是未压缩的 HTTP 连接,您可以使用 Wireshark 跟踪数据包。您可能会发现在 XML 响应数据的开头有一个意外的 XML BOM 标头(其他问题)。例如,如果您使用的 HTTP 库不支持 http 分块或 xml 编码错误,则可能会发生这种情况。

在您使用数据包嗅探器分析流量并识别 BOM 标头(或缺少 BOM 标头)之前,我们不会知道。在任何情况下,如果出现问题,您可以破解流来解释 BOM 标头。

于 2012-09-17T20:29:00.330 回答
0

使用 sax 解析,您可以阅读inputstream.

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
ParseCustomer parseEventsHandler=new ParseCustomer();
sp.parse(ivyHttp.getInputStream(),parseEventsHandler);

ParseCustomer是将读取 xml 的解析类。给定样本是:

class ParseCustomer extends DefaultHandler{
    @Override
    public void startDocument() throws SAXException {

        super.startDocument();
    }
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

        super.startElement(uri, localName, qName, attributes);//enter code here
        if (qName.equals("frameit")) {
            // compare element name and get value/ for further help read saxparser
            orderId=atts.getValue("orderId");
        }
        System.out.println(qName);

    }

    @Override
    public void endElement(String uri, String localName, String qName)
        throws SAXException {

        super.endElement(uri, localName, qName);
    }

    @Override
    public void characters(char[] ch, int start, int length)
        throws SAXException {

        super.characters(ch, start, length);
    }

}
于 2014-06-11T07:11:22.423 回答