2012-09-17 40 views
1

嗨,大家目前正在研究需要解析xml文档以验证用户的应用程序。使用java.net。*包的URLConnection类来连接到特定的URL以xml格式返回其响应。当我尝试解析使用JDOM文档时,我得到以下错误:
org.jdom2.input.JDOMParseException: Error on line 1: Premature end of fileXML从输入流解析Java

谁能找出问题所在,并协助我有办法补救吗?谢谢,这里是我的部分代码

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!", "")); 
} 
+1

这个问题可以简单地通过InputStream中返回的数据是错误格式XML - 你检查的实际内容? –

+0

正如@mattb所述,您检索的内容可能不是有效的XML文档。为了解析HTML页面,您可以使用更灵活的库,例如JSoup,请参阅:http://jsoup.org – Alex

+0

或者您可以使用流畅的API“RestAssured”或另一个名为“RestEasy”的项目... – djangofan

回答

1

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

直到您使用数据包嗅探器分析流量并确定BOM头(或缺少BOM头)时,我们才会知道。在任何情况下,如果出现问题,您可以对该流进行破解以考虑BOM头。

+0

感谢djangofan,我怎么用这个来解决这个问题? – bugiman

+0

是否意味着问题是由xml响应中的编码声明造成的? – bugiman

+0

这就是我的想法,但有可能是另一个原因。您需要将数据包跟踪它以确认,或者在将流传递给构建器之前将其打印到控制台。 – djangofan

3

看起来像它,因为你正在阅读输入流两次。一旦打印,然后建立文档。当您到达构建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())); 
0
Using sax parse you can read the inputstream. 
SAXParserFactory spf = SAXParserFactory.newInstance(); 
SAXParser sp = spf.newSAXParser(); 
ParseCustomer parseEventsHandler=new ParseCustomer(); 
sp.parse(ivyHttp.getInputStream(),parseEventsHandler); 

Here ParseCustomer is the parse class that will read xml. Given sample is: 

class ParseCustomer extends DefaultHandler{ 
      @Override 
     public void startDocument() throws SAXException { 
      // TODO Auto-generated method stub 
      super.startDocument(); 
     } 
      @Override 
     public void startElement(String uri, String localName, String qName, 
       Attributes attributes) throws SAXException { 
      // TODO Auto-generated method stub 
      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 { 
      // TODO Auto-generated method stub 
      super.endElement(uri, localName, qName); 
     } 

      @Override 
     public void characters(char[] ch, int start, int length) 
       throws SAXException { 
      // TODO Auto-generated method stub 
      super.characters(ch, start, length); 
     } 

     }